此函数作用是生成一个四元数表示的三维朝向,然后可以直接把这个朝向赋给游戏对象来变更其朝向,也可以通过线性插值(Quaternion.Slerp 和 Quaternion.Lerp)来实现游戏对象从当前朝向转到这个新生成的朝向上来。很方便也很好玩的东西,可就是两个参数理解起来太让人头大了……
官网对此函数的参数和解释如下:Quaternion.LookRotation static function LookRotation (forward : Vector3, upwards : Vector3 = Vector3.up) :
QuaternionDescriptionCreates a rotation that looks along forward with the the head upwards along upwardsLogs an error if the forward direction is zero.
矢量减法,如下图。

如伪代码:
this.FightDir = Vector3.zero;
this.FightDir = AttackRole.transform.position - base.transform.position;
this.FightDir = new Vector3(this.FightDir.x, 0f, this.FightDir.z);
this.FightDir = this.FightDir.normalized;
if (this.FightDir != Vector3.zero)
{
base.transform.rotation = Quaternion.LookRotation(this.FightDir);
}
本文详细介绍了Unity中Quaternion.LookRotation函数的使用方法及其参数意义,该函数用于生成表示三维朝向的四元数,方便游戏对象朝向的快速设置与平滑过渡。
1万+

被折叠的 条评论
为什么被折叠?



