Unity Vector3.Slerp使用范例
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class TestSlerp : MonoBehaviour 6 { 7 public Transform a; 8 public Transform b; 9 10 void Start() 11 { 12 13 } 14 bool kaishi = false; 15 float T; 16 void Update() 17 { 18 if (Input.GetKeyDown(KeyCode.S)) 19 { 20 kaishi = true; 21 T = Time.time; 22 print(Time.time + " SSSSSSSSSSS " + T); 23 24 } 25 if (kaishi) 26 { 27 Debug.DrawLine(Vector3.zero, new Vector3(10, 10, 10), Color.red); 28 //for (int i=0;i<11;++i) 29 //{ 30 Vector3 center = (a.position + b.position) * 0.5f; 31 center -= new Vector3(0, 2, 0); 32 Vector3 temp1 = a.position - center; 33 Vector3 temp2 = b.position - center; 34 print(Time.time + " " + T); 35 Vector3 c = Vector3.Slerp(temp1, temp2, (Time.time-T) * 0.1f); 36 37 c += center; 38 transform.position = c; 39 //Debug.DrawRay(center, c, Color.red); 40 //Debug.DrawRay(Vector3.zero, c, Color.red); 41 Debug.DrawLine(center, c, Color.blue); 42 Debug.DrawLine(Vector3.zero, c, Color.blue); 43 44 // print(i); 45 } 46 } 47 }
最终效果如下:
修改如下:
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class TestSlerp : MonoBehaviour { 6 public Transform a; 7 public Transform b; 8 void Start () { 9 10 } 11 void Update () { 12 Debug.DrawLine(Vector3.zero,new Vector3(10,10,10),Color.red); 13 for (int i=0;i<11;++i) 14 { 15 Vector3 center = (a.position + b.position) *0.5f; 16 center -= new Vector3(0,2,0); 17 Vector3 temp1 =a.position - center; 18 Vector3 temp2 =b.position - center; 19 Vector3 c = Vector3.Slerp(temp1, temp2, i * 0.1f); 20 Debug.DrawLine(center, c, Color.blue); 21 Debug.DrawLine(Vector3.zero, c, Color.blue); 22 } 23 } 24 }
效果如下:
更多可参考:http://www.manew.com/thread-43314-1-1.html