This commit is contained in:
huyulong 2024-12-13 19:29:43 +08:00
parent 65018a1dd2
commit 93ec2f5d86

View File

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