【unity2D】API-学习记录12-四元数类Quaternion

目标

了解并能够应用某些方法来旋转物体


API对Quaternion的(部分)说明

Quaternion Description 描述 Quaternions are used to represent rotations. 四元数用于表示旋转。 They are compact, don't suffer from gimbal lock and can easily be interpolated. 它们结构紧凑,不受万向锁影响,可以轻松插值。 Unity internally uses Quaternions to represent all rotations. Unity 内部使用四元数来表示所有旋转。 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); 您几乎不会有机会访问或修改单个四元数分量(x、y、z、w); most often you would just take existing rotations (e.g. from the Transform) 大多数情况下,您只需要获取现有旋转(例如,来自 Transform), and use them to construct new rotations (e.g. to smoothly interpolate between two rotations). 然后使用它们构造新的旋转 (例如,在两个旋转之间平滑插值)。 The Quaternion functions that you use 99% of the time are: Quaternion.LookRotation, Quaternion.Angle, Quaternion.Euler, Quaternion.Slerp, Quaternion.FromToRotation, and Quaternion.identity. 您绝大多数时间使用的四元数函数为: Quaternion.LookRotation、Quaternion.Angle、Quaternion.Euler、Quaternion.Slerp、Quaternion.FromToRotation 和 Quaternion.identity。 (The other functions are only for exotic uses.) (其他函数仅用于一些十分奇特的用例。) Note that Unity expects Quaternions to be normalized. 注意,Unity 使用的是标准化的四元数。

API中列举了在绝大多数时候使用到的四元数方法,下面来总结它们的用法。


常用的四元数方法

方法 函数原型 函数解释
Euler public static Quaternion Euler (float x, float y, float z); 返回一个旋转,它分别绕 z、x、y轴旋转 z、x、y 度(按该顺序应用)
Angle public static float Angle(Quaternion a, Quaternion b); 返回两个旋转之间的角度(以度为单位)。
FromToRotation public static Quaternion FromToRotation (Vector3 fromDirection, Vector3 toDirection); 创建一个从 fromDirection 旋转到 toDirection 的旋转。通常情况下,使用该方法对Transform进行旋转,使其的一个轴(例如 Y 轴)跟随世界空间中的目标方向
LookRotation public static Quaternion LookRotation (Vector3 forward, Vector3 upwards= Vector3.up); 使用指定的 forward 和 upwards 方向创建旋转。Z 轴将与forward对齐,X 轴与 forward 和 upwards 之间的差积对齐,Y 轴与 Z 和 X 之间的差积对齐。如果 forward 或 upwards 量值为零 或 forward 和 upwards 共线,则返回恒等。

做2D游戏的旋转,上表中的前三个方法用得比较多,尤其是Euler,非常省事。

做3D游戏用得到第四个方法。

参考资料

Unity API Quaternion(EN)
Unity API Quaternion(CN)


__EOF__

本文作者OtusScops
本文链接https://www.cnblogs.com/OtusScops/p/14758413.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   AshScops  阅读(199)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示