- Home /
Unity 2D: my LookRotation keeps rotating on the wrong axis
I'm trying to rotate a 2D sprite towards another 2D sprite on the same plane / Z location. For some reason with the following code, the sprite seems to rotate on the X and Y instead of the Z and I can't figure out why. It should ONLY rotate on the Z. Sometimes when I tweak with it, the sprite dissappears or doesn't rotate at all.
//Rotate
Vector3 relativePos = targetLocation - transform.position;
Quaternion rotation = Quaternion.LookRotation(relativePos, Vector3.up);
transform.rotation = rotation;
targetLocation is a vector3 transform.position sent from another object.
Answer by fuego_see_money · Oct 07, 2021 at 12:47 AM
Even though it seems like it should work in 2D, Unity still uses 3D coordinates and rotations when operating in 2D. This means that when you use Quaternion.LookRotation()
it will act as if the sprite is just a 3D transform and point it towards the other sprite in a 3D-orientation.
Try this instead:
Vector3 relativePos = target.position - transform.position;
float angle = Mathf.Atan2(relativePos.y, relativePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
This will grab the 2D angle you want and apply it on the forward axis (facing into the screen), which will rotate the direction you want it to. I'm not sure how your sprites are oriented "forward", so you might need to rotate it by 90 degrees more. You should be able to figure that part out though.
Wow! This is the shortest answer and most functional I've seen! Can you elaborate on why you used the commands they did, and what they do?
Thank you, and sure!
Line 1 is calculating the difference between the target's position and the script object's position, pointing from the script object towards the target.
Line 2 leverages the inverse tangent function to grab the angle given the y and x values of the normal trig function Tan. If you haven't taken trig yet or aren't fresh on your trig functions, I would recommend reading up on that so you can better understand exactly what its doing. In short summary, we use the vector calculated in Line 1 because using the x/y values of a "difference" vector will provide the right values for us to plug into the Mathf.Atan2
function. This is because Tan(angle) = Opposite/Adjacent
, and given a difference vector we can always construct a right triangle where Opposite = y
and Adjacent = x
.
Line 3 is where we both set and construct a rotation using Quaternion.AngleAxis. This will rotate angle degrees around the supplied vector. We use Vector3.forward
here because in standard Unity2D setup, the forward vector points into the screen. You can try to make sense of this by visualizing how a sprite rotates in a normal 2D game, or an image editor program like paint.NET, how it is always facing the screen. Rotating around the vector pointing into the screen will achieve this same exact result.
Hope this helps!
Your answer
Follow this Question
Related Questions
Rotating a 2D sprite to face a target on a single axis. 3 Answers
More specific Quaternion.LookRotation 1 Answer
need help regarding rotating arm 0 Answers
Confused with LookRotation in C# 1 Answer