- Home /
2D rotate object towards mouse?
So I got this code snippet off another answer but for some reason its always off by exactly 90 degrees. On line 9 I added a hot fix(just takes the angle and minus 90) but it still bothers me anyone know why?
public var angle : float;
function Update () {
var mousePos = Input.mousePosition;
mousePos.z = 10.0f; //The distance from the camera to the player object
var lookPos : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
lookPos = lookPos - transform.position;
angle = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
angle -= 90;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
}
Comment
maybe the rotation for the actual gameobject is not the angle you think it is?
Best Answer
Answer by steakpinball · Nov 16, 2013 at 02:28 AM
The original code, without the 90° shift, assumes the mouse is directly right of the object when no rotation is to be applied.