diff --git a/xiaofang/Assets/Script/npc/RecuseNpc.cs b/xiaofang/Assets/Script/npc/RecuseNpc.cs index b410a3eb..896636f8 100644 --- a/xiaofang/Assets/Script/npc/RecuseNpc.cs +++ b/xiaofang/Assets/Script/npc/RecuseNpc.cs @@ -89,51 +89,31 @@ public class RecuseNpc : MonoBehaviour private void Update() { - - // 继续处理NPC的状态和动画 - switch (nstate) + if (targetPoints.Count > 0) { - case Npcstate.idle: - SetAni(2); - break; - case Npcstate.run: - movebool = true; - Run(currentTarget); - break; - case Npcstate.dead: - SetAni(0); - break; - } - if (targetPoints.Count > 0) // 确保列表中有目标点 - { - - currentTarget = targetPoints[0]; // 获取当前的目标点 + currentTarget = targetPoints[0]; nstate = Npcstate.run; movebool = true; - float dis = Vector3.Distance(this.transform.position, currentTarget); - // 判断是否到达目标点 - if (movebool && dis < 0.1f) + + float dis = Vector3.Distance(transform.position, currentTarget); + + // 停止条件改为 NavMeshAgent.stoppingDistance + if (movebool && dis <= navMeshAgent.stoppingDistance) { Debug.Log("到达目标点"); - - // 设置NPC状态 - nstate = Npcstate.idle; movebool = false; - // 达到目标后,从列表中移除该目标点 if (targetPoints.Count > 1) { targetPoints = RemoveDes(); } else { - targetPoints.Clear(); // 清空列表 + targetPoints.Clear(); } } } - - } //移除第一个点 @@ -154,8 +134,17 @@ public class RecuseNpc : MonoBehaviour { if (movebool) { - SetAni(1); - navMeshAgent.SetDestination(target); // 使用 NavMeshAgent 设置目标点 + // 确保目标点在 NavMesh 上 + NavMeshHit hit; + if (NavMesh.SamplePosition(target, out hit, 1.0f, NavMesh.AllAreas)) + { + navMeshAgent.SetDestination(hit.position); + SetAni(1); + } + else + { + Debug.LogError($"目标点 {target} 不在导航网格上"); + } } }