# Checking rotations

Discussion in 'Scripting' started by davidnibi, May 13, 2021.

1. ### davidnibi

Joined:
Dec 19, 2012
Posts:
342
I'm having a few troubles relating to euler rotations.
Put simply if I want to check whether the current rotation is at '0', but it is actually at '-4.325711e-06', which is -0.000004325711 and if Unity is being rightly mathematically pedantic, it does not equal '0'.

I am getting the initial rotation amount, which is set a '0', and it returns the above value everytime.

Is the correct way of figuring out if the rotation is '0' on this axis is use a fuzziness and add 0.01 ie

Code (CSharp):
1. rotationFuzziness = 0.01;
2.
3. if (rotation < (initialRotation + rotationFuzziness))
4. ...
Code (CSharp):
1.     bool CheckForkMaxHeight ()
2.     {
3.         return (currentEulerAngle > (maxRotation + heightFuzziness));
4.
5.     }
BTW, the rotation works, I'm trying to figure out a shorthand way of comparison, and if you have better ways of doing it - I am converting Quaternions to euler angles.
Thanks.

Joined:
Dec 19, 2012
Posts:
342
3. ### Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
16,904
I like your thinking. They use an arbitrary epsilon that I don't think you can change.

I prefer this construct:

Code (csharp):
1. const float myEpsilon = 0.01f;
2.
3. if (Mathf.Abs( quantity1 - quantity2) < myEpsilon)
4. {
5.   Debug.Log( "Close enough for gummint work.");
6. }

davidnibi likes this.
4. ### davidnibi

Joined:
Dec 19, 2012
Posts:
342
Thanks, I was reading about Epsilon. There's this as well which runs really well:

Code (CSharp):
1.     bool CompareWithThreshold (float a, float b, float threshold)
2.     {
3.         return ((a - b) < 0 ? ((a - b) * -1) : (a - b)) <= threshold;
4.     }

5. ### Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
16,904
I goggle at code like that... do not want!

(It actually does exactly what mine does, except less-than vs less-than-or-equal)

I would imagine that also has two branches in it instead of just one.

But hey, your codebase sir!

6. ### Not_Sure

Joined:
Dec 13, 2011
Posts:
2,950
If it’s a matter of it being finicky you could always check a range.

Code (CSharp):
1. if (transform.rotation.x > -0.0001f && transform.rotation.x < 0.0001f) DoTheThing();

davidnibi likes this.
7. ### Kurt-Dekker

Joined:
Mar 16, 2013
Posts:
16,904
davidnibi likes this.
8. ### Lurking-Ninja

Joined:
Jan 20, 2015
Posts:
7,070
You really don't want to put branching in a generalized float equality check though...

unityunity