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): rotationFuzziness = 0.01; if (rotation < (initialRotation + rotationFuzziness)) ... Code (CSharp): bool CheckForkMaxHeight () { return (currentEulerAngle > (maxRotation + heightFuzziness)); } 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.
Just discovered this cool Mathf function - I wonder how 'similar' is similar though? https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html
I like your thinking. They use an arbitrary epsilon that I don't think you can change. I prefer this construct: Code (csharp): const float myEpsilon = 0.01f; if (Mathf.Abs( quantity1 - quantity2) < myEpsilon) { Debug.Log( "Close enough for gummint work."); }
Thanks, I was reading about Epsilon. There's this as well which runs really well: Code (CSharp): bool CompareWithThreshold (float a, float b, float threshold) { return ((a - b) < 0 ? ((a - b) * -1) : (a - b)) <= threshold; }
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!
If it’s a matter of it being finicky you could always check a range. Code (CSharp): if (transform.rotation.x > -0.0001f && transform.rotation.x < 0.0001f) DoTheThing();
Except you do not want to do anything with the .x field of a Quaternion. Perhaps you could do stuff with the .eulerAngles.x field however. https://docs.unity3d.com/ScriptReference/Quaternion.html Those are NOT the same thing.