- Home /
transform.localEulerAngles.y = 180 but not less-or-equal 180.0f
I need some help here. I read the y angle of a transform and test it against
private float camStartAngleY;
camStartAngleY = transform.localEulerAngles.y;
if (camStartAngleY < 0.0f) camStartAngleY += 360.0f; // make sure value stays between 0 and 360
else if (camStartAngleY > 360.0f) camStartAngleY -= 360.0f;
Debug.Log ("camStartAngleY: " + camStartAngleY);
if (camStartAngleY < 180.0f || camStartAngleY == 180.0f)
{
Debug.Log ("<=180: " + camStartAngleY);
}
Debug.Log says:
camStartAngleY: 180
UnityEngine.Debug:Log(Object)
So what is going on here?
I had to change your title as a "<" sign totally messes up the HT$$anonymous$$L parsing of the site.
You do know you can use this. Save yourself the extra ||.
if (camStartAngleY <= 180.0f)
I noticed the messed up HT$$anonymous$$L but had no idea what caused it. Thanks for fixing it.
The || I used after
Answer by tanoshimi · Dec 19, 2014 at 08:13 AM
Floating point numbers are inherently imprecise, so camStartAngleY == 180.0f
is not a reliable test.
Right, the value is most likely greater than 180. Try using
Debug.Log ("camStartAngleY: " + camStartAngleY.ToString("N8"));
To actually display all decimal digits.
Thanks. That is good to know.
The odd thing is that I had a limit variable to which I assigned exactly 180.0f and then set transform.localEulerAngles.y to it's value.
camStartAngleY = transform.localEulerAngles.y; then again gives this inprecice value. Even floats are digital and can not be inprecise on their own. $$anonymous$$y guess is, that unity is doing some internal calculations which result in a slightly inprecise transform.localEulerAngles.y. That internal stuff is not easy to predict. One tends to see something like transform.localEulerAngles.y as "just a variable" but it is more than that. It is managed by unity and therefore unity can change it too.
"Even floats are digital and can not be inprecise on their own" -> You're right in saying that a given binary value is exact and can not be, in itself, "imprecise". What's imprecise is the ability of that binary floating point value to represent a given decimal number.
@antx: That's simply because Unity stores the rotation of the object in a Quaternion and not as eulerangles. If you assign something to eulerAngles it will be converted into a Quaternion. When you read it back the Quaternion will be converted into an eulerAngles representation.
Also keep in $$anonymous$$d that you can't assign a single value to "transform.localEulerAngles.y" since localEulerAngles is a property of a value type (Vector3) which wouldn't propergate changes back to it's origin (since it's not a reference type).
UnityScript (Unity's "Javascript") actually allows this by implicitly using a temp Vector3 variable.
Unity provides [$$anonymous$$athf.Approxomately][1]
to check for imprecise equality of floating point numbers. It is preferred to using ==
to check equality.
[1]: http://docs.unity3d.com/ScriptReference/$$anonymous$$athf.Approximately.html
Your answer
Follow this Question
Related Questions
can I change float to vector3 on euler ?? 1 Answer
Reduce 2 numbers at the same rate 1 Answer
zero is not zero 2 Answers
Can't equalize float angle 1 Answer