Unity NavMesh导航报错“SetDestination“ can only be called on an active agent that has been placed on a Na

在使用NavMeshAgent进行导航的时候,发现调用SetDestination一直报错,报错显示是:

"SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)

但是查看地图,发现Bake过了,Agent也在地图上啊,照理说会自动关联到NavMesh地图上的。

后来发现是因为NavMeshAgent不能直接设置position因为那样他不知道他现在在哪里。

如果您创建一个 NavMeshAgent 并通过 transform.position=... 设置它的位置,然后尝试 SetDestination,它会失败,因为 NavMeshAgent 没有识别位置变化并且不知道它已经在 NavMesh 上。在调用 SetDestination 之前,使用 NavMeshAgent.Warp 而不是 transform.position 来初始化位置。

错误代码:

character.transform.position = pos;

正确代码:

character.Warp(pos);

参考链接:

"SetDestination" can only be called on an active agent that has been placed on a NavMesh. - Unity AnswersUnity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.https://answers.unity.com/questions/507534/setdestination-can-only-be-called-on-an-active-age-1.html

猜你喜欢

转载自blog.csdn.net/egostudio/article/details/124385862