- Home /
Question by
InevBetrayal · Jan 02, 2014 at 03:00 PM ·
mouserotatecursor
unity2D: make object face mouse
I currently have code working that a game object will follow the mouse (rotating towards it) and it works fine, unless i change the position of the object (using rigidbody2D.velocity).
void Update () {
Vector3 mouseScreen = Input.mousePosition;
Vector3 mouse = Camera.main.ScreenToWorldPoint(mouseScreen);
transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y, mouse.x) * Mathf.Rad2Deg - 90);
}
Any ideas on what i can change? Thanks!
Comment
Best Answer
Answer by robhuhn · Jan 02, 2014 at 03:05 PM
It should work if you map the coordinates to local space (not tested):
transform.rotation = Quaternion.Euler(0, 0, Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x) * Mathf.Rad2Deg - 90);
That's perfect, exactly what i needed! Thank you so much!
Your answer
Follow this Question
Related Questions
Character always facing mouse cursor position... 3 Answers
Movement along X and Z axis... 2 Answers
Changing cursor over multiple objects 0 Answers