Unity中解决“SetDestination“ can only be called on an active agent that has been placed on a NavMesh(代码片
Posted Hello Bug.
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity中解决“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,这个报错信息的重点其实并不在导航组件是否active
二:原因
游戏运行后使用Instantiate方式将实例化物体,再改变其初始位置,使得物体在实例化的一瞬间距离导航网格过远,使得导航失效
GameObject go = Instantiate(prefab);
go.transform.position = new Vector3(1, 2, 3);
三:解决方法(三种)
——在实例化的同时设置位置
GameObject go = Instantiate(prefab, new Vector3(1, 2, 3), Quaternion.identity);
——将物体身上的导航组件先关闭,在设置好位置后在开启
——判断物体是否在导航网格上,如果不在就纠正位置
if(agent.isOnNavMesh)
{
agent.Warp(pos);
}
以上是关于Unity中解决“SetDestination“ can only be called on an active agent that has been placed on a NavMesh(代码片的主要内容,如果未能解决你的问题,请参考以下文章