Debug of z-axis-rotation is not 0, while in inspector it is 0

此生再无相见时 提交于 2021-01-28 01:54:25

问题


In the inspector the z-axis rotation of my transform is 0.

But when i debug this using these lines of code:

Debug.Log("local-Euler-z: " + transform.localEulerAngles.z);
Debug.Log("global-Euler-z: " + transform.eulerAngles.z);
Debug.Log("local-rot-z: " + transform.localRotation.z);
Debug.Log("global-rot-z: " + transform.rotation.z);

you can see in my screenshot that i don't get 0 as value.


回答1:


First of all, values you're getting are really small. xE+/-y is an exponential floating point number representation.

So in reality you have something like this: 6.349661E-05 == 0.00006349661 which is quite close to actual zero.

This problem comes from float numbers precision and because there's actually 2 zeros (-0 & +0) for float numbers in programming - that's just how things work. I don't want to overload this answer, if you want to read something about floats - the link above is a good starting point.

Because of this float numbers' problem same number can be represented by close but still different float type representation. float->int is not really good for cases where you want to compare, let's say, non-int degrees, plus you can't compare Quaternion numbers like that - its components (x, y, z, w) have values between 0 and 1. To solve this problem you either need to manualy "compare" floats with machine zero which is kinda complicated for a novice, another solution is to use Mathf.Approximately from Unity API.


By the way, transform inspector shows you pure zero because it just discards such non-affecting digits of the rotation vector.



来源:https://stackoverflow.com/questions/53805407/debug-of-z-axis-rotation-is-not-0-while-in-inspector-it-is-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!