Rotating while Key pressed turns value negative once it reaches 180

I have some code here that when the Space key is held down it rotates. Rotation on the z axis starts at 0. Then gradually rises to 180. As soon as it hits 180, it turns into -179, I have figured out that it is subtracting 360 at some point.

if (Input.GetKey(KeyCode.Space))
{
aimMode = false; 
transform.Rotate(Vector3.forward * rotateSpeed * Time.deltaTime); 
}

Can someone explain this to me?

Euler angles are like that. If you need an unambiguous angle, keep your own in a float and then use it to drive the transform how you want, generally with the Quaternion.Euler() factory method.

Here’s more reading:

https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html

2 Likes