- Home /
2D game look at mouse rotate
Hi. Iv'e been trying to make this work all day, and would greatly appreciate some help! Im making a 2D birds eye view game. There is a sprite with a script attached, and I want him to rotate on the Z axis to point at my mouse cursor. Right now with my code though, he becomes invisible to the camera because t=he starts rotating on the Y and X axis! Please help much confused, thanks. CODE:
//vector for current mouse position(works)
var mouse = Vector2(Input.mousePosition.x,Input.mousePosition.y);
Debug.Log(mouse);
//does something..
var relativePos = mouse - transform.position;
//sets sprite rotation to relativePos
transform.rotation = Quaternion.LookRotation(relativePos);
Answer by robertbu · Feb 15, 2014 at 02:03 AM
This question has come up a lot since the new 2D stuff was introduced. Here is one answer:
http://answers.unity3d.com/questions/603757/2d-mouse-aiming.html
Note the solution at this link assumes that the right side of your sprite is forward and you are using an Orthographic camera.
Your answer
Follow this Question
Related Questions
Player Arm will not rotate when moving with Xbox Joystick 1 Answer
problem with rotation 2D 1 Answer
How to get click position in Vector 2 ? 1 Answer
Billboard script restrictions 1 Answer