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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼蛋-Felix

如果对你有用,可以请我喝杯可乐

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值