问题描述
在检查器中,我的变换的 z 轴旋转为 0.
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);
你可以在我的截图中看到我没有得到 0 作为值.
you can see in my screenshot that i don't get 0 as value.
推荐答案
首先,您获得的价值确实很小.xE+/-y
是一个 指数浮点数字表示.
First of all, values you're getting are really small. xE+/-y
is an exponential floating point number representation.
所以实际上你有这样的东西:6.349661E-05 == 0.00006349661
,它非常接近实际的零.
So in reality you have something like this: 6.349661E-05 == 0.00006349661
which is quite close to actual zero.
这个问题来自浮点数精度,因为在编程中浮点数实际上有 2 个零 (-0 & +0) - 这就是事情的工作原理.如果您想阅读有关浮点数的内容,我不想让这个答案超载 - 上面的链接是一个很好的起点.
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.
由于这个 float
数字的问题,相同的数字可以用接近但仍然不同的 float
类型表示来表示.float->int
对于您想要比较的情况并不是很好,比如说,非整数度,而且您不能像这样比较 Quaternion
数字 -它的组件 (x, y, z, w)
的值介于 0 和 1 之间.要解决这个问题,您要么需要手动比较"浮点数与机器零,这对新手来说有点复杂,另一个解决方案是使用 Mathf.Approximately
来自 UnityAPI.
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.
这篇关于z-axis-rotation 的调试不是 0,而在检查器中它是 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!