UnityCommon/Role/move/2D/SimplePathfindingDoTween.cs
2024-12-04 05:50:47 +08:00

37 lines
1.1 KiB
C#

using UnityEngine;
using DG.Tweening; // 引入DoTween命名空间
public class SimplePathfindingDoTween : Fun
{
public Transform[] waypoints; // 预设的路径点
public float moveDuration = 1f; // 每个点之间的移动时长
private int currentWaypointIndex = 0; // 当前路径点的索引
//void Start()
//{
// // 让角色开始移动到第一个路径点
// MoveToNextWaypoint();
//}
void MoveToNextWaypoint(GameObject gameObject)
{
if (currentWaypointIndex < waypoints.Length)
{
// 获取下一个路径点
Transform targetWaypoint = waypoints[currentWaypointIndex];
// 使用DoTween实现平滑移动到目标位置
gameObject.transform.DOMove(targetWaypoint.position, moveDuration)
.OnComplete(() => MoveToNextWaypoint(gameObject)); // 移动完成后继续到下一个路径点
currentWaypointIndex++; // 更新路径点索引
}
else
{
Debug.Log("到达终点!");
}
}
}