transform.localEulerAngles.y off by 0.0001 problem

I have the following bit of code run:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFlip : MonoBehaviour {
   
   [SerializeField]
   private GameObject gameManager;
   private GameManager gameManagerScript;
   [SerializeField]
   private GameObject player;
   private Player playerScript;
   [SerializeField]
   private GameObject light;
   [SerializeField]
   private float tiltSpeed = 1f;
   
   protected bool rotating;
   
   private void Start() {
       playerScript = player.GetComponent<Player>();
       gameManagerScript = gameManager.GetComponent<GameManager>();
   }
   
   private void Update() {
       Debug.Log(transform.localEulerAngles.y);
   }
   
   public void Rotate() {
       if (transform.localEulerAngles.y == 0) {
           StartCoroutine(FlipNormalToReverse());
           playerScript.reversedControls = true;
       } else if (transform.localEulerAngles.y == 180f) {
           StartCoroutine(FlipReverseToNormal());
           playerScript.reversedControls = false;
       }
   }
   
   IEnumerator FlipNormalToReverse() {
       while (transform.localEulerAngles.y <= 180f) {
           transform.Rotate(Vector3.up, tiltSpeed);
           light.transform.Rotate(Vector3.up, tiltSpeed);
           rotating = true;
           yield return new WaitForSeconds(0.1f);
       }
       if (transform.localEulerAngles.y == 180f) {
           rotating = false;
       }
   }
   
   IEnumerator FlipReverseToNormal() {
       while (transform.localEulerAngles.y >= 180f) {
           transform.Rotate(Vector3.up, tiltSpeed);
           light.transform.Rotate(Vector3.up, tiltSpeed);
           rotating = true;
           yield return new WaitForSeconds(0.01f);
       }
       if (transform.localEulerAngles.y == 0) {
           rotating = false;
       }
   }
}

It likes to output values like 40.00001 and 180.001.
Should I just go ahead and run some code to truncate these digits, or is there some underlying issue I should be aware of?

I am running Unity version 5.6.2f1.

Yes and yes. :stuck_out_tongue:

The problem is a combination of floating point precision and Unity’s internal representation of rotations as quaternions instead of euler angles.

There isn’t much you can do about it so just ignore it. And make sure you have a small fudge factor to use when comparing rotations.

I agree with the above post - generally not a good idea to compare floats as they’re not precise enough. You can use an approximate comparison which helps: Unity - Scripting API: Mathf.Approximately

Thanks. I will try using that in the future.