欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2024/3/12 编程问答 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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

总结

以上是生活随笔为你收集整理的Unity NavMesh导航报错“SetDestination“ can only be called on an active agent that has been placed on a Na的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。