81 lines
2.0 KiB
C#
81 lines
2.0 KiB
C#
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 Transform EndPos;
|
|
|
|
public Transform TypeEndPos1;
|
|
public Transform TypeEndPos2;
|
|
public Transform TypeEndPos3;
|
|
public Transform TypeEndPos4;
|
|
public Transform TypeEndPos5;
|
|
public Transform TypeEndPos6;
|
|
|
|
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 void StartMove()
|
|
{
|
|
|
|
|
|
// 提取路径中的所有点的位置
|
|
|
|
for (int i = 0; i < pathPoints.Length; i++)
|
|
{
|
|
if (PlayerMovePos.instance.StartPos == pathPoints[i])
|
|
{
|
|
startIndex = i;
|
|
Debug.Log("Startindex"+i);
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
Vector3[] waypoints = new Vector3[pathPoints.Length-startIndex];
|
|
for (int i=startIndex,j=0;j< pathPoints.Length&&i< pathPoints.Length; i++,j++)
|
|
{
|
|
|
|
waypoints[j]=pathPoints[i].position;
|
|
|
|
}
|
|
Debug.Log("数组起始点" + waypoints[0]);
|
|
// 使用 DoTween 的 DOPath 方法创建路径动画
|
|
Tween pathTween = objectToMove.DOPath(waypoints, duration, PathType.Linear)
|
|
.SetEase(Ease.Linear) // 线性过渡效果
|
|
.SetLoops(isLooping ? -1 : 0)
|
|
.OnComplete(AfterMove);
|
|
|
|
}
|
|
|
|
|
|
public void AfterMove()
|
|
{
|
|
|
|
PlayerMovePos.instance.StartPos =EndPos;
|
|
Debug.Log("修改StartPos值" + PlayerMovePos.instance.StartPos);
|
|
}
|
|
} |