- Home /
Quaternion.LookRotation Returning Undesired Rotation
I have a top-down 2D game (constrained to xy-plane), and I'm trying to use Quaternion.LookRotation to rotate the player based on the position of the mouse.
Here is a screen recording of the problem: video.
I suspect the transformation is giving me undesired results since my up and look vectors for the camera are different than default.
// This code is in the FixedUpdate() of the PlayerBehavior component
// attached to the player object.
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 lookVector = (mousePosition - transform.position);
lookVector.z = 0.0f;
transform.rotation = Quaternion.LookRotation(lookVector, new Vector3(0.0f, 0.0f, -1.0f));
// Since I only want the player to be rotated with respect to the z-axis,
// the x and y components of the rotation are zeroed out.
transform.eulerAngles = new Vector3(0.0f, 0.0f, transform.eulerAngles.z);
The resulting transformation rotates my player in unusual, unexpected ways that do not resemble my desired result. The intention is for the mouse to rotate the player similar to games like Hotline Miami.
For reference, my up vector is (0, 0, -1), my camera is at (0, 0, -1), my camera's look vector is (0, 0, 1).
So I found a working solution for my problem, but I'm not sure why it works:
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// Z is aligned to (transform.position - mousePosition)
// Y is aligned to (0, 0, 1)
Quaternion rot = Quaternion.LookRotation(transform.position - mousePosition, Vector3.forward);
// Remove the x and y components of the orientation
rot.x = 0.0f;
rot.y = 0.0f;
// Fully rotate to the new orientation
transform.rotation = Quaternion.Slerp(transform.rotation, rot, 1.0f);
Answer by robertbu · Sep 18, 2014 at 11:24 AM
Directly manipulating the components of a Quaternion is problematic. The most common solution posted on Unity Answers (and it's been posted many times) is as follows:
Vector3 screenPos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 dir = Input.mousePosition - screenPos;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
This code assumes that the 'forward' side of your sprite is to the right.
@robertbu Thanks for the answer robert, my bad if this has been answered several times before, I did make a conscious effort to search for similar posts before posting. Your code works great, but I did have to multiply the angle by -1 in order for it to work. I am trying to step through why that's the case.
Figured out why the angle needed to be negative: the rotation is expected to be counter-clockwise and my up vector is (0, 0, -1) since my game is a top-down 2D game. Using my up vector in the AngleAxis call produces a clockwise rotation if not multiplied by -1.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Top-down movement in 2D 1 Answer
Multiply quaternion by vector3... how is it done (mathematically)? 1 Answer
Character looking at opposite direction of mouse 1 Answer
2D top-down movement... 6 Answers