首先在場景中創建三個cube的GameObject,from表示要轉換之前的樣子,to表示轉換之后的樣子,change表示轉的效果。如下圖所示:
其中from和change cube開始運行之前的transform是一樣的。to cube的transform如下圖所示:
然后我們創建一個腳本Quat.cs,如下:
1 public class Quat : MonoBehaviour { 2 [SerializeField] 3 public Transform from; 4 5 [SerializeField] 6 public Transform to; 7 8 private float rotSpeed = 5.0f; 9 void Update () { 10 if (Input.GetMouseButtonDown(0)) 11 { 12 Prx(from.rotation); 13 Prx(to.rotation); 14 rotSpeed += 4; 15 transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, rotSpeed*Time.deltaTime); 16 } 17 } 18 19 public void Prx(Quaternion q) 20 { 21 Debug.Log(string.Format("x={0} y={1} z={2} w={3}", q.x, q.y, q.z, q.w)); 22 } 23 }
我們把該腳本賦change cube,並且把from和to cube拖到相應的手槽里。然后運行時,我們點擊鼠標左鍵,可以看到change cube慢慢旋轉成to cube的角度。如下所示:
點一次插入一個值,偏移一點點,直到和to cube的角度一樣。