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);
}
Use the Transform.LookAt function in LateUpdate for that..
– Imankitankit.tiks007 The problem with the Transform.LookAt is that it immediately snaps on the mouse click, and I want the object to smootly turns :)... Or is it there anyway to do that with LookAt?
– Andrey86You can probably use something like iTween.RotateTo().
– dandagoThanks for the answer dandago! I got a hint from another guy that my turning should be updated, because the click is too short time wise. Maybe I can add a boolean, or loop until a condition is met. I will try that tonight.
– Andrey86dandago, I have never heard of iTween.RotateTo() I will check it out in the manual. Thanks man! ;)
– Andrey86