Unity3d mimic World of Warcraft mouse-to-game operation

Source: Internet
Author: User

1. Create a new Unity3d project and import the Charactercontroller package into the project. Create plane as the ground in the game and set the plane tag to ground. Create directional light to illuminate the game world. Put the third-person controller on the plane, then remove the third-person script and set its tag to player.

2. Create a Looktargetpos script and attach it to the third-person controller. The function is that when the left mouse button is pressed and the position being pressed is plane, the third-person controller moves toward the mouse-down direction.

usingUnityengine;usingSystem.Collections; Public classlooktargetpos:monobehaviour{ Public StaticVector3 Targetpos;//used to save the mouse click to the ground position    Private BOOLIsmousedown =false;//determine if the left mouse button has been pressed    voidStart () {Targetpos= This. transform.position; }    //Update is called once per frame    voidUpdate () {if(Input.getmousebuttondown (0) ) {Ismousedown=true;        Lookatpos (); }        Else if(Input.getmousebuttonup (0) ) {Ismousedown=false; }        //if the left mouse button is pressed all the time, the mouse position is updated        if(Ismousedown = =true) {lookatpos (); }    }    voidLookatpos () {ray Ray=Camera.main.ScreenPointToRay (input.mouseposition);        Raycasthit Hitinfo; BOOLIscollider = Physics.raycast (Ray, outhitinfo); //determine if the mouse clicks on the ground        if(Iscollider = =true&& Hitinfo.collider.tag = ="Ground") {Targetpos=Hitinfo.point; Targetpos.y= This. transform.position.y;  This. Transform.        LookAt (Targetpos); }    }}

3. Create a Playermove script and attach it to the third-person controller. Its role is to control the third-person controller to move to the left mouse button press position.

usingUnityengine;usingSystem.Collections; Public enumplayerstate{moveing, Idle} Public classplayermove:monobehaviour{PrivateCharactercontroller Controller;  Public intSpeed =4; Private floatDistance//to preserve the distance between the protagonist and the target .     PublicPlayerstate State;//Save the status of the game lead//Use this for initialization    voidStart () {Controller= This. Getcomponent<charactercontroller>(); State=Playerstate.idle; }    //Update is called once per frame    voidUpdate () {Move (); }    voidMove () {Distance= Vector3.distance ( This. Transform.position, Looktargetpos.targetpos); if(distance>0.05f) {controller. Simplemove ( This. Transform.forward *Speed ); State=playerstate.moveing;        print (distance); }        Else{ state=Playerstate.idle; }    }}

4. Create a playerstate script and attach it to the third-person controller. Its role is to control the third-person controller animation playback.

usingUnityengine;usingSystem.Collections; Public classplayerstate:monobehaviour{PrivatePlayermove playerstate; Private floatdistance; //Use this for initialization    voidStart () {playerstate= This. Getcomponent<playermove>(); }    //Update is called once per frame    voidlateupdate () {//if the protagonist is moving, play the running animation        if(Playerstate.state = =playerstate.moveing) {playanimation ("Run"); }        //If the game protagonist is waiting on the animation of the playback station        Else if(Playerstate.state = =playerstate.idle) {playanimation ("Idle"); }    }    voidPlayanimation (stringanimationname) {animation.    Crossfade (Animationname); }}

5. Create a Camerafollow script and mount it on the main camera. Its function is: 1, let main camera to follow the third-person controller movement, 2, change the main camera view, 3, control the main camera and the third-person controller distance.

usingUnityengine;usingSystem.Collections; Public classcamerafollow:monobehaviour{PrivateVector3 Followpos;//used to preserve the relative position of the camera and the protagonist of the game    PrivateGameobject player;  Public floatScrollspeed =Ten;//speed of the camera closer    Private BOOLIsrotating =false;//determine if the right mouse button is pressed     Public floatRotatespeed =2;//the speed at which the camera rotates around    Private floatDistance//save camera and game lead distance//Use this for initialization    voidStart () {player= Gameobject.findgameobjectwithtag ("Player"); Followpos= Player.transform.position- This. transform.position;  This. Transform.    LookAt (player.transform.position); }    //Update is called once per frame    voidUpdate () {follow ();        Rotateview ();    ScrollView (); }    //make the camera follow the game lead movement    voidFollow () { This. transform.position = Player.transform.position-Followpos; }    //when you slide the mouse wheel, you can change the distance between the camera and the main player.    voidScrollView () {Distance=Followpos.magnitude; Distance+ = Input.getaxis ("Mouse Scrollwheel"); Distance=mathf.clamp (Distance,2, -); Followpos= followpos.normalized *distance; }    //move the mouse when you press the right mouse button to change the camera's perspective    voidRotateview () {if(Input.getmousebuttondown (1) ) {isrotating=true; }        Else if(Input.getmousebuttonup (1) ) {isrotating=false; }        if(isrotating) { This. Transform. Rotatearound (Player.transform.position, Player.transform.up, Rotatespeed * Input.getaxis ("Mouse X")); Vector3 Originalpos= This. transform.position; quaternion originalrotation= This. transform.rotation;  This. Transform. Rotatearound (Player.transform.position, This. Transform.right,-rotatespeed * Input.getaxis ("Mouse Y")); floatx = This. transform.eulerangles.x;            print (x); //limit the maximum and minimum position of camera rotation            if(x<Ten|| X> -)            {                 This. transform.position =Originalpos;  This. transform.rotation =originalrotation; }} Followpos= Player.transform.position- This. transform.position; }}

6, after the completion of the effect, screen mouse can not see, sorry.

Unity3d mimic World of Warcraft mouse-to-game operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.