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

143 lines
4.3 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-18 16:55:00 +08:00
public float moveSpeed; // <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-14 21:15:33 +08:00
2024-12-19 19:01:39 +08:00
public Tween pathTween;
2024-12-20 21:41:40 +08:00
public float speedFactor=1;//<2F>ٶ<EFBFBD><D9B6><EFBFBD><EFBFBD><EFBFBD>
2024-12-18 16:55:00 +08:00
2024-12-14 21:15:33 +08:00
[Header("<22><>ת<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>")]public GameObject obj;
2024-12-18 17:49:14 +08:00
public void MoveToNextWaypoint(GameObject gameObject, float moveSpeed)
{
2024-12-11 16:51:55 +08:00
if (currentWaypointIndex < waypoints._waypoints.Count)
2024-12-04 12:10:07 +08:00
{
2024-12-18 17:49:14 +08:00
// <20><>ȡĿ<C8A1><C4BF>·<EFBFBD><C2B7><EFBFBD><EFBFBD>
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-18 17:49:14 +08:00
float timeToReach = distance * 10 / moveSpeed; // <20><><EFBFBD><EFBFBD><EFBFBD>ٶȿ<D9B6><C8BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>ʱ<EFBFBD><CAB1>
// <20><>ȡ·<C8A1><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>
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-18 17:49:14 +08:00
if (i!=0&&i!= path.Length-1)
{
// <20><>Y<EFBFBD><EFBFBD><E1B7BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<C4B1><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Χ
path[i].y += Random.Range(-0.1f, 0.1f); // <20>޸<EFBFBD> Y ֵ<>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>Χ
}
2024-12-04 12:10:07 +08:00
}
2024-12-04 05:50:39 +08:00
2024-12-18 17:49:14 +08:00
// ʹ<><CAB9>DoTween<65><6E>DOPath<74><68>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
2024-12-19 19:01:39 +08:00
pathTween=gameObject.transform.DOPath(path, timeToReach, PathType.Linear)
2024-12-18 17:49:14 +08:00
.SetEase(Ease.Linear)
2024-12-20 21:41:40 +08:00
.OnUpdate(() => {
2024-12-24 14:10:25 +08:00
//pathTween.timeScale = speedFactor;
2024-12-20 21:41:40 +08:00
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
}
2024-12-14 21:15:33 +08:00
public void PauseAnimation()
{
// <20><>ͣ<EFBFBD><CDA3><EFBFBD><EFBFBD>
transform.DOPause();
}
2024-12-19 19:01:39 +08:00
2024-12-20 21:41:40 +08:00
// <20><><EFBFBD><EFBFBD>DoTween<65><6E><EFBFBD>ٶȣ<D9B6><C8A3><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>µ<EFBFBD><C2B5>ٶ<EFBFBD><D9B6><EFBFBD><EFBFBD>Ӻ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ָ<EFBFBD>ʱ<EFBFBD><CAB1>
public void ChangeSpeed(float newSpeedFactor, float restoreTime)
{
2024-12-24 14:10:25 +08:00
// <20><>ǰ<EFBFBD><C7B0>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
float currentSpeedFactor = pathTween.timeScale;
2024-12-19 19:01:39 +08:00
2024-12-24 14:10:25 +08:00
// ʹ<><CAB9>Tween<65><6E>ƽ<EFBFBD><C6BD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD>ٶ<EFBFBD><D9B6><EFBFBD><EFBFBD><EFBFBD>
DOTween.To(() => currentSpeedFactor, x => pathTween.timeScale = x, newSpeedFactor, restoreTime)
.OnKill(() =>
{
Debug.Log("<22><><EFBFBD>ü<EFBFBD><C3BC><EFBFBD>+++++++++++++++++");
// <20><><EFBFBD>ӳٽ<D3B3><D9BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>ԭ<EFBFBD>ٶ<EFBFBD>
2024-12-20 21:41:40 +08:00
pathTween.timeScale = 1f; // <20>ָ<EFBFBD>ԭ<EFBFBD>ٶ<EFBFBD>
});
}
2024-12-24 10:58:35 +08:00
private bool isTweenPaused = false; // <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>Ѿ<EFBFBD><D1BE><EFBFBD>ͣ
private float accumulatedStopTime = 0f; // <20>ۼƵ<DBBC><C6B5><EFBFBD>ͣʱ<CDA3><CAB1>
public void StopPathDoTween(float stopTime) // <20><>ͣ<EFBFBD>ƶ<EFBFBD>
2024-12-19 19:01:39 +08:00
{
2024-12-24 10:58:35 +08:00
if (isTweenPaused)
{
// <20><><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD>ͣ<EFBFBD><CDA3><EFBFBD>ۼ<EFBFBD><DBBC>µ<EFBFBD><C2B5><EFBFBD>ͣʱ<CDA3><CAB1>
accumulatedStopTime += stopTime;
return;
}
2024-12-19 19:01:39 +08:00
// <20><>ͣ<EFBFBD><CDA3>ǰ<EFBFBD>Ķ<EFBFBD><C4B6><EFBFBD>
pathTween.Pause();
2024-12-24 10:58:35 +08:00
isTweenPaused = true;
2024-12-19 19:01:39 +08:00
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ٣<D3B3><D9A3><EFBFBD>ֱʱ<D6B1>
DOTween.To(() => 0f, x => { }, 0f, stopTime).OnKill(() =>
{
// <20><><EFBFBD>ӳٽ<D3B3><D9BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>
pathTween.Play();
2024-12-24 10:58:35 +08:00
isTweenPaused = false;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۻ<EFBFBD><DBBB><EFBFBD><EFBFBD><EFBFBD>ͣʱ<CDA3><EFBFBD><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ
if (accumulatedStopTime > 0f)
{
// ʹ<><CAB9><EFBFBD>ۼƵ<DBBC><C6B5><EFBFBD>ͣʱ<CDA3><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͣ
StopPathDoTween(accumulatedStopTime);
accumulatedStopTime = 0f; // <20><><EFBFBD><EFBFBD><EFBFBD>ۻ<EFBFBD>ʱ<EFBFBD><CAB1>
}
2024-12-19 19:01:39 +08:00
});
}
2024-12-24 10:58:35 +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>
2024-12-14 21:15:33 +08:00
obj.transform.rotation = Quaternion.Euler(0, 0, 0);
2024-12-11 16:51:55 +08:00
}
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>
2024-12-14 21:15:33 +08:00
obj.transform.rotation = Quaternion.Euler(0, 180, 0);
2024-12-11 16:51:55 +08:00
}
2024-12-04 05:50:39 +08:00
}
2024-12-04 12:10:07 +08:00
}