一,欧拉角转换成四元数

Quaternion.Euler(欧拉角)


二,四元数转换成欧拉角

Quaternion qt = this.transform.rotation;

Vector3 euler = qt.eulerAngles;


三,轴角旋转

Quaternion qt = Quaternion.AngleAxis( 50 , Vector3.up );

相当于:Quaternion.Euler( 0 , 50, 0);


 四,注视旋转

ps:z轴注视一个方向

1⃣️,没有缓动

this.transform.rotation = Quaternion.LookRotation(targetVec - this.transform.position);

相当于:this.transform.LookAt(targetVec - this.transform.postion);

2⃣️,先快后慢

Quaternion targetQt = Quaternion.LookRotation(targetVec - this.transform.position);

this.transform.rotation = Quaternion.Lerp( this.transform.rotation,targetQt, 0.1f );

3⃣️,匀速

Quaternion targetQt = Quaternion.LookRotation(targetVec - this.transform.position);

this.transform.rotation = Quaternion.RotateTowards( this.transform.rotation,targetQt, 0.1f );


五,角度差值(角度差)

float a = Quaternion.Angle( this.transform.rotation,targetQt );

if( a < = 2 ){

    this.transform.rotation = targetQt;

}


六, x轴注视

1⃣️,right(无缓动)

this.transform.right = targetVec - this.transform.position;

2⃣️,FromToRotation(无缓动)

this.transtorm.rotation = Quaternion.FromToRotation( Vector3.right, targetVec );

例子:

Unity3D之Quaternion API_Unity

                ①,分析,如下图所示

Unity3D之Quaternion API_API_02

                            a, 首先垂直向下打一条射线

                            b, 通过此射线能够得到强面的法向量

                            c,得到法向量, .....代码如下:

    void Update()
    {
        if (Input.GetMouseButtonDown(0))//监听鼠标左键
        {
            if (Physics.Raycast(this.transform.position, -Vector3.up, out this.hit, 100, this.layerMap))//向正下方发射光线检测
            {
                Quaternion qt = Quaternion.FromToRotation(Vector3.up, this.hit.normal);//以Y轴,法线旋转
                this.transform.rotation = qt;
            }
        }
    }

3⃣️, 缓动(套Lerp)