The moving of the role of Unity3d--the mouse movement under the view angle

Source: Internet
Author: User

Similar to the game of DotA mobile method, the rest is nothing to say.

usingUnityengine;usingSystem.Collections; Public classGogogogo:monobehaviour { Public floatMovespeed;  PublicVector3 newposition;  PublicCharactercontroller Charactercontroller; //Public Animationclip Idle; //Public Animationclip Run; //Use this for initialization    voidStart () {newposition=transform.position; }        //Update is called once per frame    voidUpdate () {if(Input.getmousebutton (0) ) {locateposition ();    } movetoposition (); }    //a way to get a click on the mouse click position    voidlocateposition () {Raycasthit hit; Ray Ray .=Camera.main.ScreenPointToRay (input.mouseposition); if(Physics.raycast (Ray, outHit +)) {            if(Hit.collider.tag! ="Player") {newposition=NewVector3 (HIT.POINT.X,HIT.POINT.Y,HIT.POINT.Z); }        }    }    //a way to start moving and rotate    voidmovetoposition () {if(Vector3.distance (transform.position,newposition) >2){            //Rotatequaternion newrotation = quaternion.lookrotation (Newposition-transform.position, Vector3.forward); Newrotation.x=0f; Newrotation.z=0f; Transform.rotation= Quaternion.slerp (Transform.rotation, Newrotation, time.deltatime* -); //MovingCharactercontroller. Simplemove (Transform.forward *movespeed); //Animation. Crossfade (run.name);        }        Else        {            //Animation. Crossfade (idle.name);        }    }}

The moving of the role of Unity3d--the mouse movement under the view angle

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.