关于Unity3d的Quaternion.Slerp的学习

首先在场景中创建三个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的角度一样。

原文地址:https://www.cnblogs.com/koeltp/p/9763733.html