Unity3d-ray Control movement

Source: Internet
Author: User



1 usingUnityengine;2 usingSystem.Collections;3 4  Public classT2:monobehaviour5 {6 7     //Use this for initialization8     voidStart ()9     {Ten  One     } A  -     //define Ray - Ray Ray; the Raycasthit hit; -     //whether to move -     BOOLIsmove; -     //Move Direction + Vector3 dir; - Vector3 Pos; +  A     //Update is called once per frame at     voidUpdate () -     { -         //Float v = input.getaxis ("Vertical"); -         //float h = input.getaxis ("horizontal"); -         //transform. Translate (Transform.forward * v * time.deltatime); -  in  -         //transform. Rotate (Vector3.up * h); to  +  -         //when the left mouse button is pressed the         if(Input.getmousebuttondown (0)) *         { $             //send a ray from the cameraPanax NotoginsengRay =Camera.main.ScreenPointToRay (input.mouseposition); -             //If an object is detected (the ray is used to detect) the             if(Physics.raycast (Ray, outHit )) +             { A                 //Change the Y value of the collision point to the Y value of transform thepos =NewVector3 (Hit.point.x, TRANSFORM.POSITION.Y, hit.point.z); +  -  $                 //Hit.point is the position of the object. $ transform. LookAt (hit.point); -                 //Start Moving -Ismove =true; the                 //ask for direction of movement -                 //dir = (hit.point-transform.position). normalized;Wuyi  theDIR = (pos-transform.position). normalized; -  Wu             } -         } About         //If you start moving $         if(Ismove) -         { -             //move along a position in a world coordinate -Transform. Translate (dir * time.deltatime *3, Space.world); A             //if the distance is less than 0.5m, stop moving +             if(Vector3.distance (Transform.position, Hit.point) <0.5f) the             { -Ismove =false; $             } the         } the  the  the  -     } in}

Unity3d-ray Control movement

Related Article

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.