Hi guys,
This is killing me for hours…
I want simply to rotate an object towards the mouse cursor when I click.
The scripts works, but in its current state when I click the object rotates just a little bit. I have to click 15 times to get the object to rotate in the right position. When I use just GetMouseButton, I can hold the mouse button and the object follow the mouse fine, but I want to rotate the object at the cursor with just one click.
Thank you in Advance!
function Update () {
if (Input.GetMouseButtonDown(0)) {
Turn();
}
}
function Turn () {
var newPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hitDistance = 0.0;
newPlane.Raycast(ray, hitDistance);
var targetPoint = ray.GetPoint(hitDistance);
var newRot = Quaternion.LookRotation(transform.position - targetPoint, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, newRot, Time.deltaTime * 2);
}