C# Rotation Check

this may be a dumb question, but why does this code not work?

if(transform.rotation.z > 270 || transform.rotation.z < 90){
				transform.Rotate (-rotation);

but the object keeps rotating when it’s more than 90 or less than 270…

You are dealing with quaternion components here. You might want to use transform.rotation.eulerAngles.z

First of all, rotation is a member of Transform, so you cannot use it by itself (without transform.) for the transform.Rotate parameter. Additionally, transform.Rotate needs eulerAngles as its parameter, so it wouldn’t work with transform.rotate anyway, because transform.rotation returns a Quaternion. So, if you want to work in degrees, you should use transform.eulerAngles instead.

Use this:

Vector3 currentRotation = transform.eulerAngles;

if(currentRotation.z > 270 || currentRotation.z < 90){
transform.Rotate (-currentRotation);