- Home /
Sprite facing object to mouse
var relative : Vector2 = transform.InverseTransformPoint(Input.mousePosition);
transform.Rotate (0, 0, (Mathf.Atan2(relative.y, relative.x)*Mathf.Rad2Deg));
So the above is my code. It is supposed to make the sprite I have it on rotate (using 3dtoolbox for my sprites). Now I know that it doesn't face the mouse, but rather is rotated by a position based on the mouse in WORLD space.
Now I've used the example here : http://www.unifycommunity.com/wiki/index.php?title=LookAtMouse
The problem with it is two fold. I got it rotate the sprite around the Z axis. but if the mouse passes the halfway point in the screen (or crosses the Y axis of the object, I didn't test that properly) it flips. Also, I can't figure out what I need to do to rotate the rotation 90 degrees because instead of the "top" of the sprite it faces the left side.
I hope I explained this well enough. Anyway I'd LIKE to use my existing code, rather than someone else's code I don't understand.