SetDestination can only be called on an active agent that has been placed on a NavMesh 解决方案

问题分析

"SetDestination" can only be called on an active agent that has been placed on a NavMesh.

翻译为:“SetDestination”只能在已放置在NavMesh上的活动代理上调用。

对于这个问题 是因为 生成预制件之后的一瞬间就调用了寻路。寻路系统会判定你的代理不在地图上。因为生成预制件会进行位置的设定。

找了找原因后发现,在正确的设置好导航网格后,如果角色已经放置在导航网格上,运行游戏则正常导航,但如果角色是运行游戏后加载进场景,会触发上述报错。

原因

原因:游戏运行后使用Instantiate方式将角色载入场景后,再改变其初始位置,使得角色在载入的一瞬间距离导航网格过远,使得导航失效!

解决方案

解决办法有三种:

先检查地图是否烘焙,若没有寻路网格,在Nagation窗口点击Bake地图。

1.实例化物体时直接传入位置信息:

GameObject player = Instantiate(temp,new Vector3(30, 0, 45),Quaternion.identity);

2.实例化物体后设置所在导航网格内位置:

  1. NavMeshAgent agent = player.GetComponent<NavMeshAgent>();
  2. agent.Warp(new Vector3(30, 0, 45));

3.角色预制体上首先将NavMeshAgent组件的enable设置为false,等到需要导航时再将其设置为true。

在个人项目中 我使用方法三解决了该问题。

作者:Miracle
来源:麦瑞克博客
链接:https://www.playcreator.cn/archives/unity/1659/
本博客所有文章除特别声明外,均采用CC BY-NC-SA 4.0许可协议,转载请注明!
THE END
分享
海报
SetDestination can only be called on an active agent that has been placed on a NavMesh 解决方案
问题分析 "SetDestination" can only be called on an active agent that has been placed on a NavMesh. 翻译为:“SetDestination”只能在已放置在NavMesh上的……
<<上一篇
下一篇>>
文章目录
关闭
目 录