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)

相关文章
相关标签/搜索