I’m trying to create a simple rotation on the z-axis for my 2D game.
Quaternions and rotations are my weak points in Unity, so I know I may be asking a really simple question, I just cannot for the life of me handle this topic.
Anyway, I have an enemy that looks at the character by switching their transform.right. However, this caused the gun (the thing that I want to rotate and a child object of the enemy) to rotate weirdly when I tried to manipulate the transform.right component of the gun to look at the player.
After a bit of research, I came up with this:
if (transform.position.x > player.position.x)
{
transform.right = Vector2.left;
}
else if (transform.position.x < player.position.x)
{
transform.right = Vector2.right;
}
Quaternion rotation = Quaternion.Lerp(gun.localRotation, Quaternion.LookRotation(player.position - gun.position, gun.TransformDirection(Vector3.up)), accuracy * Time.deltaTime);
gun.localRotation = new Quaternion(0, 0, rotation.z, rotation.w);
This code works when I’m on the right side of the enemy. When I’m on the left side, the gun rotates on the z-axis with negative numbers. For example, if the player is on the right side, the rotation in the inspector would read 6. If the player was on the left side, it would read -6.
I decided to use Mathf.Abs(rotation.z) which I thought would cause the rotations to be positive, but all this did was make the gun stop rotating altogether when the player is on the left side.
Could someone please help me find a solution?
Again, I know I may be asking a simple question that may be common sense, but this is my weakpoint ![]()



