using UnityEngine; public class RotateTest : MonoBehaviour { public Transform a; public Transform b; public Transform c; void Start() { Quaternion q1 = new Quaternion(1,2,3,4); Debug.Log("創建一個四元數:"+q1);//輸出:(1.0, 2.0, 3.0, 4.0) Vector3 eulerAngle = q1.eulerAngles; Debug.Log("四元數轉歐拉角:" + eulerAngle);//輸出:(352.3, 47.7, 70.3) Quaternion q2 = Quaternion.Euler(eulerAngle); Debug.Log("歐拉角轉四元數:" + q2);//輸出:(-0.2, -0.4, -0.5, -0.7) Vector3 axis; float angle; q1.ToAngleAxis(out angle, out axis); Debug.Log("四元數轉軸向角,旋轉角度:" + angle+"旋轉軸:"+axis);//輸出:旋轉角度:86.17744旋轉軸:(0.3, 0.5, 0.8) Quaternion q3= Quaternion.AngleAxis(angle, axis); Debug.Log("軸向角轉四元數:"+q3);//輸出:(0.2, 0.4, 0.5, 0.7) //測試 a.rotation = q1; b.rotation = q2; c.rotation = q3; //結果:a,b,c的旋轉相同,inspector面板里的Rotation都是(-7.662, 47.726, 70.346) } }
