怪物移动
This commit is contained in:
parent
2b93b14699
commit
84ca89c647
@ -22,35 +22,42 @@ public class SimplePathfindingDoTween : Fun
|
|||||||
|
|
||||||
|
|
||||||
[Header("旋转的动画")]public GameObject obj;
|
[Header("旋转的动画")]public GameObject obj;
|
||||||
public void MoveToNextWaypoint(GameObject gameObject,float moveSpeed)
|
public void MoveToNextWaypoint(GameObject gameObject, float moveSpeed)
|
||||||
{
|
{
|
||||||
if (currentWaypointIndex < waypoints._waypoints.Count)
|
if (currentWaypointIndex < waypoints._waypoints.Count)
|
||||||
{
|
{
|
||||||
// 获取下一个路径点
|
// 获取目标路径点
|
||||||
Transform targetWaypoint = waypoints._waypoints[waypoints._waypoints.Count-1];
|
Transform targetWaypoint = waypoints._waypoints[waypoints._waypoints.Count - 1];
|
||||||
|
|
||||||
// 计算路径点之间的距离
|
// 计算路径点之间的距离
|
||||||
float distance = Vector3.Distance(gameObject.transform.position, targetWaypoint.position);
|
float distance = Vector3.Distance(gameObject.transform.position, targetWaypoint.position);
|
||||||
|
|
||||||
// 根据设置的速度计算时间
|
// 根据设置的速度计算时间
|
||||||
float timeToReach = distance*10 / moveSpeed; // 根据速度控制匀速移动的时间
|
float timeToReach = distance * 10 / moveSpeed; // 根据速度控制匀速移动的时间
|
||||||
|
|
||||||
// 使用DoTween的DOPath来平滑地移动到目标位置
|
// 获取路径的所有点
|
||||||
Vector3[] path = new Vector3[waypoints._waypoints.Count - currentWaypointIndex];
|
Vector3[] path = new Vector3[waypoints._waypoints.Count - currentWaypointIndex];
|
||||||
for (int i = 0; i < path.Length; i++)
|
for (int i = 0; i < path.Length; i++)
|
||||||
{
|
{
|
||||||
path[i] = waypoints._waypoints[currentWaypointIndex + i].position;
|
path[i] = waypoints._waypoints[currentWaypointIndex + i].position;
|
||||||
|
if (i!=0&&i!= path.Length-1)
|
||||||
|
{
|
||||||
|
// 在Y轴方向上做一定的随机改变,可以设置一个波动范围
|
||||||
|
path[i].y += Random.Range(-0.1f, 0.1f); // 修改 Y 值的波动范围
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 使用DoTween的DOPath来平滑地移动
|
||||||
gameObject.transform.DOPath(path, timeToReach, PathType.Linear)
|
gameObject.transform.DOPath(path, timeToReach, PathType.Linear)
|
||||||
.SetEase(Ease.Linear).OnUpdate(() => RotateTowardsTarget()); // 每次更新时调整旋转
|
.SetEase(Ease.Linear)
|
||||||
|
.OnUpdate(() => RotateTowardsTarget()); // 每次更新时调整旋转
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Log("到达终点!");
|
Debug.Log("到达终点!");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
public void PauseAnimation()
|
public void PauseAnimation()
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user