关于Unity3d的Quaternion.Slerp的学习

首先在场景中建立三个cube的GameObject,from表示要转换以前的样子,to表示转换以后的样子,change表示转的效果。以下图所示:spa

其中from和change cube开始运行以前的transform是同样的。to cube的transform以下图所示:code

而后咱们建立一个脚本Quat.cs,以下:orm

 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的角度。以下所示:blog

点一次插入一个值,偏移一点点,直到和to cube的角度同样。string

相关文章
相关标签/搜索