- Home /
How to move GameObject by mouseclick?
When I click left mouse button the object must rotate to that point and move there. Rotate by Y axis and move forward by Z axis. Like in Diablo 2. How can I do this using Javascript?
Answer by 2rusbekov · Apr 06, 2012 at 09:34 PM
Answer found.
var plrCam : Camera; var moveSpeed : int = 5; var rotateSpeed : int = 1; var targPos : Vector3; function Update () { if(Input.GetMouseButtonDown(0)) { var ray = plrCam.ScreenPointToRay (Input.mousePosition); var hit : RaycastHit; if (Physics.Raycast(ray, hit)) targPos = hit.point; } var targDir = targPos - transform.position; var targDist = Vector3.Distance(transform.position, targPos); if (targDist > 1.5) { transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(targDir), rotateSpeed * Time.deltaTime); transform.rotation.z = 0; transform.rotation.x = 0; transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); } }
Your answer
Follow this Question
Related Questions
Problem with click-to-move script 0 Answers
Move Camera According to Mouse Movement While Button is Pressed 1 Answer
How to select one prefab from more ? 0 Answers
Having a first person player look at the mouse on the y axis 1 Answer
Problem with Alt + Middle Click Drag for Camera (Windows) 3 Answers