using UnityEngine; using System.Collections; public class MoveCurve : MonoBehaviour { public GameObject t1; //开始位置 public GameObject t2; //结束位置 // Update is called once per frame void Update () { //两者中心点 Vector3 center = (t1.transform .position + t2.transform.position) * 0.5f; center -= new Vector3(0, 1, 0); Vector3 start = t1.transform.position - center; Vector3 end = t2.transform.position - center; //弧形插值 transform.position = Vector3.Slerp(start,end,Time.time); transform.position += center; } }
还有一种方法也可以实现,代码如下
1 using UnityEngine; 2 using System.Collections; 3 4 public class ProjectileTest : MonoBehaviour 5 { 6 public GameObject target; //要到达的目标 7 public float speed = 10; //速度 8 private float distanceToTarget; //两者之间的距离 9 private bool move = true; 10 11 void Start() 12 { 13 //计算两者之间的距离 14 distanceToTarget = Vector3.Distance(this.transform.position, target.transform.position); 15 StartCoroutine(StartShoot()); 16 } 17 18 IEnumerator StartShoot() 19 { 20 21 while (move) 22 { 23 Vector3 targetPos = target.transform.position; 24 25 //让始终它朝着目标 26 this.transform.LookAt(targetPos); 27 28 //计算弧线中的夹角 29 float angle = Mathf.Min(1, Vector3.Distance(this.transform.position, targetPos) / distanceToTarget) * 45; 30 this.transform.rotation = this.transform.rotation * Quaternion.Euler(Mathf.Clamp(-angle, -42, 42), 0, 0); 31 float currentDist = Vector3.Distance(this.transform.position, target.transform.position); 32 if (currentDist < 0.5f) 33 move = false; 34 this.transform.Translate(Vector3.forward * Mathf.Min(speed * Time.deltaTime, currentDist)); 35 yield return null; 36 } 37 } 38 39 40 }
转自 http://blog.csdn.net/cen616899547/article/details/41956653