Unity3D之Quaternion API

一,歐拉角轉換成四元數

Quaternion.Euler(歐拉角)c#


二,四元數轉換成歐拉角

Quaternion qt = this.transform.rotation;
ide

Vector3 euler = qt.eulerAngles;post


三,軸角旋轉

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

至關於:Quaternion.Euler( 0 , 50, 0);orm


 四,注視旋轉

ps:z軸注視一個方向blog

1⃣️,沒有緩動get

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

至關於:this.transform.LookAt(targetVec - this.transform.postion);it

2⃣️,先快後慢io

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 );

例子:

B01.png

                ①,分析,以下圖所示

B02.png

                            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)

相關文章
相關標籤/搜索