UnityCommon/Role/move/2D/SimplePathfindingDoTween.cs

66 lines
2.1 KiB
C#
Raw Normal View History

2024-12-04 12:10:07 +08:00
using UnityEngine;
2024-12-11 16:51:55 +08:00
using DG.Tweening;
using UnityEngine.UIElements;
using System.Collections.Generic; // <20><><EFBFBD><EFBFBD>DoTween<65><6E><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD>
[System.Serializable]
public class waypoints
{
public List<Transform> _waypoints = new List<Transform>();
}
2024-12-04 05:50:39 +08:00
2024-12-04 12:10:07 +08:00
public class SimplePathfindingDoTween : Fun
{
2024-12-11 16:51:55 +08:00
public waypoints waypoints = new waypoints();// Ԥ<><D4A4><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>
2024-12-04 12:10:07 +08:00
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>
2024-12-11 16:51:55 +08:00
Vector3 Vector3 = new Vector3();
2024-12-04 12:10:07 +08:00
public void MoveToNextWaypoint(GameObject gameObject)
2024-12-11 16:51:55 +08:00
{
if (currentWaypointIndex < waypoints._waypoints.Count)
2024-12-04 12:10:07 +08:00
{
// <20><>ȡ<EFBFBD><C8A1>һ<EFBFBD><D2BB>·<EFBFBD><C2B7><EFBFBD><EFBFBD>
2024-12-11 16:51:55 +08:00
Transform targetWaypoint = waypoints._waypoints[waypoints._waypoints.Count-1];
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>
2024-12-11 16:51:55 +08:00
float timeToReach = distance*10 / moveSpeed; // <20><><EFBFBD><EFBFBD><EFBFBD>ٶȿ<D9B6><C8BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>ʱ<EFBFBD><CAB1>
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>
2024-12-11 16:51:55 +08:00
Vector3[] path = new Vector3[waypoints._waypoints.Count - currentWaypointIndex];
2024-12-04 12:10:07 +08:00
for (int i = 0; i < path.Length; i++)
2024-12-04 05:50:39 +08:00
{
2024-12-11 16:51:55 +08:00
path[i] = waypoints._waypoints[currentWaypointIndex + i].position;
2024-12-04 12:10:07 +08:00
}
2024-12-04 05:50:39 +08:00
2024-12-04 12:10:07 +08:00
gameObject.transform.DOPath(path, timeToReach, PathType.Linear)
2024-12-11 16:51:55 +08:00
.SetEase(Ease.Linear).OnUpdate(() => RotateTowardsTarget()); // ÿ<>θ<EFBFBD><CEB8><EFBFBD>ʱ<EFBFBD><CAB1><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-11 16:51:55 +08:00
}
void RotateTowardsTarget()
{
Vector3 _vector3 = Vector3 - gameObject.transform.position;
Vector3 = gameObject.transform.position;
if (_vector3.x>0)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD>ұ߻<D2B1>ƽ<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>y<EFBFBD><79>תΪ180<38><EFBFBD><E3A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
}
else
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>y<EFBFBD><79>תΪ0<CEAA><EFBFBD><E3A3AC><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD>
gameObject.transform.rotation = Quaternion.Euler(0, 180, 0);
}
2024-12-04 05:50:39 +08:00
}
2024-12-04 12:10:07 +08:00
}