在环境中添加小球给小球添加组件有sphere collider\rigidbody给小球添加“create empty”三个命名为waypoint1/2/3作为轨迹的控制点。添加以下脚本BezierCurve用作贝塞尔曲线也就是我们执行移动的轨迹using System.Collections; using System.Collections.Generic; using UnityEngine; public class BezierCurve { public static Vector3 Point(Vector3 startPos, Vector3 endPos, float _t) { return startPos * (1 - _t) endPos * _t; } public static Vector3 Point(Vector3 startPos, Vector3 pos1, Vector3 endPos, float _t) { return startPos * (1 - _t) * (1 - _t) pos1 * 2 * _t * (1 - _t) endPos * _t * _t; } public static Vector3 Point(Vector3 startPos, Vector3 pos1, Vector3 pos2, Vector3 endPos, float _t) { return startPos * (1 - _t) * (1 - _t) * (1 - _t) pos1 * 3 * _t * (1 - _t) * (1 - _t) pos2 * 3 * _t * _t * (1 - _t) endPos * _t * _t * _t; } }ObjectMove脚本控制小球按照轨迹移动using System.Collections; using System.Collections.Generic; using UnityEngine; public class ObjectMove : MonoBehaviour { //运动路径的控制点 private Transform wayPoint1; private Transform wayPoint2; private Transform wayPoint3; //控制点的位置 private Vector3 position0; private Vector3 position1; private Vector3 position2; private Vector3 position3; private float time 0; //物体几秒后到达终点请在Inspector中赋值 public float lifeTime; void Awake() { wayPoint1 transform.Find(WayPoint1); wayPoint2 transform.Find(WayPoint2); wayPoint3 transform.Find(WayPoint3); } void Start() { position0 transform.position; position1 wayPoint1.position; if (wayPoint2 ! null) { position2 wayPoint2.position; } if (wayPoint3 ! null) { position3 wayPoint3.position; } } void Update() { MoveByBezireCurve(); } private void MoveByBezireCurve() { if (wayPoint2 null) { transform.position BezierCurve.Point(position0, position1, time / lifeTime); } else if (wayPoint3 null) { transform.position BezierCurve.Point(position0, position1, position2, time / lifeTime); } else { transform.position BezierCurve.Point(position0, position1, position2, position3, time / lifeTime); } time Time.deltaTime; if (time lifeTime) { Destroy(gameObject); } } private float deltaT 0.01f; private void OnDrawGizmos() { wayPoint1 transform.Find(WayPoint1); wayPoint2 transform.Find(WayPoint2); wayPoint3 transform.Find(WayPoint3); for (float t 0; t 1; tdeltaT) { if (wayPoint2 null) { Gizmos.DrawSphere(BezierCurve.Point(transform.position, wayPoint1.position, t), 0.05f); } else if (wayPoint3 null) { Gizmos.DrawSphere(BezierCurve.Point(transform.position, wayPoint1.position, wayPoint2.position, t), 0.05f); } else { Gizmos.DrawSphere(BezierCurve.Point(transform.position, wayPoint1.position, wayPoint2.position, wayPoint3.position, t), 0.05f); } } } }