2024-12-04 12:10:07 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using DG.Tweening; // <20><><EFBFBD><EFBFBD>DoTween<65><6E><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD>
|
2024-12-04 05:50:39 +08:00
|
|
|
|
|
2024-12-04 12:10:07 +08:00
|
|
|
|
public class SimplePathfindingDoTween : Fun
|
|
|
|
|
{
|
|
|
|
|
public Transform[] waypoints; // Ԥ<><D4A4><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>
|
|
|
|
|
public float moveSpeed = 1f; // <20><><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6>ٶȵIJ<C8B5><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶȣ<D9B6>
|
2024-12-04 05:50:39 +08:00
|
|
|
|
|
2024-12-04 12:10:07 +08:00
|
|
|
|
private int currentWaypointIndex = 0; // <20><>ǰ·<C7B0><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
public void MoveToNextWaypoint(GameObject gameObject)
|
2024-12-04 05:50:39 +08:00
|
|
|
|
{
|
2024-12-04 12:10:07 +08:00
|
|
|
|
if (currentWaypointIndex < waypoints.Length)
|
|
|
|
|
{
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>һ<EFBFBD><D2BB>·<EFBFBD><C2B7><EFBFBD><EFBFBD>
|
|
|
|
|
Transform targetWaypoint = waypoints[currentWaypointIndex];
|
2024-12-04 05:50:39 +08:00
|
|
|
|
|
2024-12-04 12:10:07 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD>ľ<EFBFBD><C4BE><EFBFBD>
|
|
|
|
|
float distance = Vector3.Distance(gameObject.transform.position, targetWaypoint.position);
|
2024-12-04 05:50:39 +08:00
|
|
|
|
|
2024-12-04 12:10:07 +08:00
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5>ٶȼ<D9B6><C8BC><EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
float timeToReach = distance / moveSpeed; // <20><><EFBFBD><EFBFBD><EFBFBD>ٶȿ<D9B6><C8BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>ʱ<EFBFBD><CAB1>
|
2024-12-04 05:50:39 +08:00
|
|
|
|
|
2024-12-04 12:10:07 +08:00
|
|
|
|
// ʹ<><CAB9>DoTween<65><6E>DOPath<74><68>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>Ŀ<EFBFBD><C4BF>λ<EFBFBD><CEBB>
|
|
|
|
|
Vector3[] path = new Vector3[waypoints.Length - currentWaypointIndex];
|
|
|
|
|
for (int i = 0; i < path.Length; i++)
|
2024-12-04 05:50:39 +08:00
|
|
|
|
{
|
2024-12-04 12:10:07 +08:00
|
|
|
|
path[i] = waypoints[currentWaypointIndex + i].position;
|
|
|
|
|
}
|
2024-12-04 05:50:39 +08:00
|
|
|
|
|
2024-12-04 12:10:07 +08:00
|
|
|
|
// ִ<><D6B4>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>
|
|
|
|
|
gameObject.transform.DOPath(path, timeToReach, PathType.Linear)
|
|
|
|
|
.SetEase(Ease.Linear); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2024-12-04 05:50:39 +08:00
|
|
|
|
|
2024-12-04 12:10:07 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("<22><><EFBFBD><EFBFBD><EFBFBD>յ㣡");
|
2024-12-04 05:50:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-04 12:10:07 +08:00
|
|
|
|
}
|