41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using UnityEngine;
|
|
using DG.Tweening; // 引入DoTween命名空间
|
|
|
|
public class SimplePathfindingDoTween : Fun
|
|
{
|
|
public Transform[] waypoints; // 预设的路径点
|
|
public float moveSpeed = 1f; // 控制移动速度的参数(更高值表示更快的速度)
|
|
|
|
private int currentWaypointIndex = 0; // 当前路径点的索引
|
|
|
|
public void MoveToNextWaypoint(GameObject gameObject)
|
|
{
|
|
if (currentWaypointIndex < waypoints.Length)
|
|
{
|
|
// 获取下一个路径点
|
|
Transform targetWaypoint = waypoints[currentWaypointIndex];
|
|
|
|
// 计算路径点之间的距离
|
|
float distance = Vector3.Distance(gameObject.transform.position, targetWaypoint.position);
|
|
|
|
// 根据设置的速度计算时间
|
|
float timeToReach = distance / moveSpeed; // 根据速度控制匀速移动的时间
|
|
|
|
// 使用DoTween的DOPath来平滑地移动到目标位置
|
|
Vector3[] path = new Vector3[waypoints.Length - currentWaypointIndex];
|
|
for (int i = 0; i < path.Length; i++)
|
|
{
|
|
path[i] = waypoints[currentWaypointIndex + i].position;
|
|
}
|
|
|
|
// 执行路径动画,保证匀速前进
|
|
gameObject.transform.DOPath(path, timeToReach, PathType.Linear)
|
|
.SetEase(Ease.Linear); // 设置匀速
|
|
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("到达终点!");
|
|
}
|
|
}
|
|
} |