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

When using NavMeshAgent for navigation, I found that calling SetDestination keeps reporting errors, and the error display is:

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

But looking at the map, I found that the Bake has passed, and the Agent is also on the map. It is reasonable to say that it will be automatically associated with the NavMesh map.

It was later discovered that the NavMeshAgent cannot directly set the position because then he does not know where he is now.

If you create a NavMeshAgent and set its position via transform.position=... and then try to SetDestination, it will fail because the NavMeshAgent doesn't recognize the position change and doesn't know it's already on the NavMesh. Use NavMeshAgent.Warp instead of transform.position to initialize position before calling SetDestination.

error code:

character.transform.position = pos;

Correct code:

character.Warp(pos);

Reference link:

"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

Guess you like

Origin blog.csdn.net/egostudio/article/details/124385862