0

I am trying to check if my object's z rotation value comes between two given values. As shown below, the value to check is between -1 and +1. When my object is in negative such as -0.5, it is never called. Works perfect for positive values above 0, but is never called when it is under 0.

void Update () {
   if (transform.eulerAngles.z <= 1 && transform.eulerAngles.z >= -1) {
            Debug.Log ("object is in between");
        }
}

Okay so I found the problem, when I added a Debug.Log (transform.eulerAngles.z); The values lesser than 0, starts from 360 instead of printing negative values. How do I fix it?

Programmer
  • 39
  • 5
  • That would work if `transform.eulerAngles.z` is -0.5 so something else must be wrong. – Matthew Watson Jan 11 '22 at 14:04
  • @MatthewWatson This is the only thing running in my script. I have even tried using localEulerAngles. But the result is the same, I am confused as to why this is happening. – Programmer Jan 11 '22 at 14:15
  • This code works with angles smaller 0. Did you check whether Update() is called in this case? Please add the rest of your script. – d4zed Jan 11 '22 at 14:22
  • When the angle is less than 0, the message is never called. The above code is the only thing that is in my script. Nothing else. I am rotating the z-axis of the object with hand. – Programmer Jan 11 '22 at 14:23
  • I have found the issue. Lesser than 0 values, starts from 360. That is, -1 is 359. I have updated my question. How do i fix this? – Programmer Jan 11 '22 at 14:26
  • I'm not sure if there is something to fix. This is probably how the transform euler angles in unity work. You can, of course, convert the eulerAngle range, before your if statement, or adapt it. – d4zed Jan 11 '22 at 14:32
  • Wondering if [Normalise orientation between 0 and 360](https://stackoverflow.com/questions/1628386/normalise-orientation-between-0-and-360) is helpful? – Wyck Jan 11 '22 at 14:46

2 Answers2

2

eulerAngles in general are always a bit unreliable for certain things.

The reason is that internally Unity doesn't store these but stores rotations as Quaternion. However, there are multiple ways of how to represent a Quaternion in euler space and eulerAngles just decides for the best human readable.

When you read the .eulerAngles property, Unity converts the Quaternion's internal representation of the rotation to Euler angles. Because, there is more than one way to represent any given rotation using Euler angles, the values you read back out may be quite different from the values you assigned.

Easier in my eyes would be to not think in rotation angles at all.

An equal way to make your check rather simply vector based would be checking if the Vector3.Angle between the global Vector3.up vector and your local transform.up is within your threshold

if(Vector3.Angle(Vector3.up, transform.up) <= 1f)
{
    Debug.Log ("object is in between");
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
0

Since Euler Angles in Unity work this way (less than 0 is basically less than 360), I changed my code as:

if ((transform.eulerAngles.z >= 0 && transform.eulerAngles.z <= 1f) || (transform.eulerAngles.z >= 358f && transform.eulerAngles.z <= 360f)) {
            Debug.Log("object is in between");
        }
Programmer
  • 39
  • 5