using System.Collections; using UnityEngine; using UnityEngine.UI; using DG.Tweening; public class PlayerMove : MonoBehaviour { public Transform objectToMove; // 要移动的对象 public Transform[] pathPoints; // 路径上的点的数组 public float duration = 5f; // 移动的持续时间 public bool isLooping = false; // 是否循环路径 int startIndex = 0; int endIndex = 0; public float Speed = 2.5f; public Transform EndPos; public Transform TypeEndPos1; public Transform TypeEndPos2; public Transform TypeEndPos3; public Transform TypeEndPos4; public Transform TypeEndPos5; public Transform TypeEndPos6; public Transform JumpPos; private void Start() { } public bool JudgeEnd(Transform pos) { if (pos==TypeEndPos1|| pos == TypeEndPos2 || pos == TypeEndPos3 || pos == TypeEndPos4 || pos == TypeEndPos5 || pos == TypeEndPos6 ) { return true; } return false; } public bool JudagePos1() { if (EndPos==TypeEndPos1) { return true ; } return false; } public void StartMove() { // 提取路径中的所有点的位置 for (int i = 0; i < pathPoints.Length; i++) { if (PlayerMovePos.instance.StartPos == pathPoints[i]) { startIndex = i; Debug.Log("Startindex"+i); } if (EndPos == pathPoints[i]) { endIndex = i; Debug.Log("Endindex" + i); } } if (endIndex>startIndex) { duration=(endIndex-startIndex)/Speed; Vector3[] waypoints = new Vector3[endIndex-startIndex+1]; for (int i=startIndex,j=0;j< pathPoints.Length&&i<=endIndex; i++,j++) { if (!JudagePos1() && pathPoints[i]==JumpPos) { waypoints[j] = pathPoints[i - 1].position; continue; } waypoints[j]=pathPoints[i].position; } OnMove(waypoints); } else if(endIndex < startIndex) { duration = (startIndex-endIndex) / Speed; Vector3[] waypoints = new Vector3[startIndex-endIndex + 1]; for (int i = startIndex, j = 0; j < pathPoints.Length && i >= endIndex; i--, j++) { if (!JudagePos1() && pathPoints[i] == JumpPos) { waypoints[j] = pathPoints[i+1].position; continue; } waypoints[j] = pathPoints[i].position; } OnMove(waypoints); } } public void AfterMove() { PlayerMovePos.instance.StartPos =EndPos; Debug.Log("修改StartPos值" + PlayerMovePos.instance.StartPos); objectToMove.DOMoveY(objectToMove.position.y+100,0.5f); } public void OnMove(Vector3[] waypoints) { // 使用 DoTween 的 DOPath 方法创建路径动画 Tween pathTween = objectToMove.DOPath(waypoints, duration, PathType.Linear) .SetEase(Ease.Linear) // 线性过渡效果 .SetLoops(isLooping ? -1 : 0) .OnComplete(AfterMove); } }