Look
Code:
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