前篇链接:Unity之C#学习笔记(3):“吃掉”道具——碰撞事件的基本处理 Collision
在一个物体的监察面板中,Transform组件的第二项Rotation就是物体的旋转,三个值分别是物体沿xyz轴旋转的角度,称为欧拉角(Euler Angle)。但是如果我们像xyz坐标那样用这三个值来表示物体的旋转,则可能出现万向节死锁(Gimbal Lock)、插值不平滑等问题。
在Unity中,旋转的完整表示不是由3个,而是4个数字:x, y, z, w构成的,而且前三个值也不等于欧拉角。这种结构称为四元数Quaternion。让我们先来看看官方文档是怎么说的。
Description
They are based on complex numbers and are not easy to understand intuitively. You almost never access or modify individual Quaternion components (x,y,z,w); most often you would just take existing rotations (e.g. from the Transform) and use them to construct new rotations (e.g. to smoothly interpolate between two rotations).
大概解释一下。四元数并不容易直观理解,但幸运的是,我们几乎不需要单独处理某个四元数中x, y, z, w的数值(实际上官方也不建议你这么做),而一般只需获取一个现存的四元数值,然后用它构建一个新的旋转。四元数的原理涉及到了图形学的知识,将来如果写到OpenGL部分博主再做具体介绍。如果你是刚入门Unity的小白,可以暂时不用理解它的原理。
官方也说明了会常用到的四元数方法:
The Quaternion functions that you use 99% of the time are: Quaternion.LookRotation, Quaternion.Angle, Quaternion.Euler, Quaternion.Slerp, Quaternion.FromToRotation, and Quaternion.identity. (The other functions are only for exotic uses.)