0

I am trying to check if my object's y rotation is 0 or not. If yes, the bool should be enabled. But this never happens even when the object's rotation is (0, 0, 0). What am I doing wrong?

public bool isZero = false;

void Update () {
        if(transform.eulerAngles.y == 0){
            isZero = true;
        } else {
            isZero = false;
         }
    }
Nijako
  • 15
  • 4
  • See also: https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html – UnholySheep Jun 14 '23 at 14:17
  • no sorry i am still not able to follow this. – Nijako Jun 14 '23 at 14:27
  • 3
    You cannot compare floating point value reliably using `==`. You need to use something like `Mathf.Approximately(transform.eulerAngles.y, 0.0f)` instead – UnholySheep Jun 14 '23 at 14:30
  • isZero = Mathf.Abs(transform.eulerAngles.y) < 0.1f; // adjust 0.1f to your liking – Dan Byström Jun 14 '23 at 14:30
  • But what if in the inspector window it is 0 but when I debug transform.eulerAngles.y, it comes as 90. Why does this happen? – Nijako Jun 14 '23 at 14:36
  • I suggest you look at this: https://stackoverflow.com/questions/73753781/how-do-i-get-a-game-objects-rotation-values-that-is-displayed-in-the-editor-in unity is doing shady things in the background with transform.eulerAngles – Nathan Jun 14 '23 at 14:46
  • 1
    What do you actually want to do? Comparing one component of euler angles is generally insignificant. – shingo Jun 14 '23 at 15:06
  • @Nathan it's not "shady things" ^^ there is simply many (maybe even infinite?) ways to represent a Quaternion in Euler space -> unity goes with what they consider the best human readable values – derHugo Jun 14 '23 at 16:53

0 Answers0