Problem with transform.rotation and if statement

I have a bunch of prefabs that either have a positive or negative rotation.z (from the values I read in the inspector)

But whenever I try to test the rotation.z in an if statement as such:

if (thePrefab.transform.rotation.z < 0) {
         //  do some code
}

… This if statement is never true for some reason.

I also tried with transform.eulerAngles.z, but it didn’t work either.

How can I make sure this if statement is true when prefabs whose rotation is shown as negative in the inspector?

Thanks in advance

1 Answer

1

First off, stick with transform.eulerAngles, transform.rotation will give you quaternions and quaternions are weird.

I just threw together a quick test. It’s because Unity will automatically convert negative angles into their positive counterpart. -90 degrees will become 270 degrees instead. Instead, if you want to check for negative rotation, why not ask if (transform.eulerAngles.z > 180)?

Yes it worked, thanks for that!