From bd7bc9cfad21045ad385db645eb08b708dde73e2 Mon Sep 17 00:00:00 2001 From: shurongsen <1799096798@qq.com> Date: Wed, 4 Dec 2024 12:10:07 +0800 Subject: [PATCH] add --- Role/Role.cs | 8 ++-- Role/move/2D/SimplePathfindingDoTween.cs | 57 +++++++++++++----------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/Role/Role.cs b/Role/Role.cs index 45945a8..b7be7c2 100644 --- a/Role/Role.cs +++ b/Role/Role.cs @@ -161,10 +161,10 @@ public class Role : Fun // 如果启用 MonoBehaviour,则每个固定帧速率的帧都将调用此函数 private void FixedUpdate() { - if (!isAnimationPlay) - { - updateAnimation(); - } + //if (!isAnimationPlay) + //{ + // updateAnimation(); + //} } diff --git a/Role/move/2D/SimplePathfindingDoTween.cs b/Role/move/2D/SimplePathfindingDoTween.cs index da58bf6..ed43c2b 100644 --- a/Role/move/2D/SimplePathfindingDoTween.cs +++ b/Role/move/2D/SimplePathfindingDoTween.cs @@ -1,36 +1,41 @@ +using UnityEngine; +using DG.Tweening; // 引入DoTween命名空间 - using UnityEngine; - using DG.Tweening; // 引入DoTween命名空间 +public class SimplePathfindingDoTween : Fun +{ + public Transform[] waypoints; // 预设的路径点 + public float moveSpeed = 1f; // 控制移动速度的参数(更高值表示更快的速度) - public class SimplePathfindingDoTween : Fun + private int currentWaypointIndex = 0; // 当前路径点的索引 + + public void MoveToNextWaypoint(GameObject gameObject) { - public Transform[] waypoints; // 预设的路径点 - public float moveDuration = 1f; // 每个点之间的移动时长 - - private int currentWaypointIndex = 0; // 当前路径点的索引 - - //void Start() - //{ - // // 让角色开始移动到第一个路径点 - // MoveToNextWaypoint(); - //} - - public void MoveToNextWaypoint(GameObject gameObject) + if (currentWaypointIndex < waypoints.Length) { - if (currentWaypointIndex < waypoints.Length) - { - // 获取下一个路径点 - Transform targetWaypoint = waypoints[currentWaypointIndex]; + // 获取下一个路径点 + Transform targetWaypoint = waypoints[currentWaypointIndex]; - // 使用DoTween实现平滑移动到目标位置 - gameObject.transform.DOMove(targetWaypoint.position, moveDuration) - .OnComplete(() => MoveToNextWaypoint(gameObject)); // 移动完成后继续到下一个路径点 + // 计算路径点之间的距离 + float distance = Vector3.Distance(gameObject.transform.position, targetWaypoint.position); - currentWaypointIndex++; // 更新路径点索引 - } - else + // 根据设置的速度计算时间 + float timeToReach = distance / moveSpeed; // 根据速度控制匀速移动的时间 + + // 使用DoTween的DOPath来平滑地移动到目标位置 + Vector3[] path = new Vector3[waypoints.Length - currentWaypointIndex]; + for (int i = 0; i < path.Length; i++) { - Debug.Log("到达终点!"); + path[i] = waypoints[currentWaypointIndex + i].position; } + + // 执行路径动画,保证匀速前进 + gameObject.transform.DOPath(path, timeToReach, PathType.Linear) + .SetEase(Ease.Linear); // 设置匀速 + + } + else + { + Debug.Log("到达终点!"); } } +} \ No newline at end of file