using System.Collections; using System.Collections.Generic; using UnityEngine; using ChivaXR; using System; using DG.Tweening; using Sirenix.OdinInspector; using QFramework; using UnityEngine.PlayerLoop; public class AniDriver_MoveByPath : AnimationDriverBase { [LabelText("打开路径")] public bool openLinePath; [LabelText("需要移动对象")] public Transform movePlayer; [LabelText("移动路径点")] public List pathPoints = new List(); //public float moveSpeed = 4; //public float rotateSpeed = 4; public float duration; public override void FinishedState() { movePlayer.position = pathPoints[pathPoints.Count - 1].position; } public override void InitState() { movePlayer.position = pathPoints[0].position; } public override void StartPlay(Action finishedCallBack = null) { StartAniCoroutine(MovePathByItem(finishedCallBack)); } public override void OnDrawGizmos() { if (!openLinePath) return; if (pathPoints == null || pathPoints.Count <= 1) return; for (int i = 0; i < pathPoints.Count; i++) { if (i + 1 < pathPoints.Count) { Gizmos.color = Color.red; Gizmos.DrawLine(pathPoints[i].position, pathPoints[i + 1].position); } } } ///// ///// 根据路径点移动 ///// ///// ///// ///// ///// ///// //IEnumerator MovePathByItem(Action finishedCallBack = null) //{ // for (int i = 0; i < pathPoints.Count; i++) // { // bool moveToTarget = false; // while (!moveToTarget) // { // movePlayer.transform.position = Vector3.MoveTowards(movePlayer.transform.position, pathPoints[i].position, moveSpeed * Time.deltaTime); // movePlayer.transform.rotation = Quaternion.Lerp(movePlayer.transform.rotation, pathPoints[i].rotation, rotateSpeed * Time.deltaTime); // if (Vector3.Distance(movePlayer.transform.position, pathPoints[i].position) < 0.02f && Quaternion.Angle(movePlayer.transform.rotation, pathPoints[i].rotation) < 5) // { // movePlayer.transform.position = pathPoints[i].position; // movePlayer.transform.rotation = pathPoints[i].rotation; // moveToTarget = true; // } // yield return new WaitForEndOfFrame(); // } // } // Debug.Log("MoveEnd is true"); // finishedCallBack?.Invoke(); //} /// /// 根据路径点移动 /// /// /// /// /// /// IEnumerator MovePathByItem(Action finishedCallBack = null) { //总距离 float tmpTotalLength = 0; for (int i = 0; i < pathPoints.Count - 1; i++) tmpTotalLength += Vector3.Distance(pathPoints[i].position, pathPoints[i + 1].position); //平均时间 = 总时长/总距离 float averageTime = duration / tmpTotalLength; for (int i = 0; i < pathPoints.Count - 1; i++) { //初始位置 Vector3 tmpInitPosition = pathPoints[i].position; //初始角度 Quaternion tmpInitQuaternion = pathPoints[i].rotation; bool moveToTarget = false; float timer = 0; //移动指定距离所需时间 float tmpTime = Vector3.Distance(pathPoints[i].position, pathPoints[i + 1].position)*averageTime; float lerp = 0; while (!moveToTarget) { movePlayer.transform.position = Vector3.Lerp(tmpInitPosition, pathPoints[i + 1].position, lerp); movePlayer.transform.rotation = Quaternion.Lerp(tmpInitQuaternion, pathPoints[i + 1].rotation, lerp); if (Vector3.Distance(movePlayer.transform.position, pathPoints[i + 1].position) < 0.02f && Quaternion.Angle(movePlayer.transform.rotation, pathPoints[i + 1].rotation) < 5) { movePlayer.transform.position = pathPoints[i + 1].position; movePlayer.transform.rotation = pathPoints[i + 1].rotation; moveToTarget = true; } timer += Time.deltaTime; lerp = timer / tmpTime; yield return new WaitForEndOfFrame(); } } Debug.Log("MoveEnd is true"); finishedCallBack?.Invoke(); } }