- Home /
Nav mesh if agent was above the ground
I am creating a little battle royale prototype and AI is parachuting, and then is using nav mesh to move/wander. Error occurs when I set Destination(from other script) and while Warp-AI doesn't move AND of course it caused an error. What I tried is to disable nav mesh agent while AI was above the ground, but it did not help. Code snippet(I disabled nav mesh from start as well):
Behavior behavior;//behaviour script which uses nav mesh for movement
UnityEngine.AI.NavMeshAgent agent; //agent itself
public bool AiOnLand()
{
return Physics.Raycast(transform.position, -Vector3.up, distanceToGround + 0.1f); //check if AI is on the ground
}
if (AiOnLand() == true)//understandable
{
behavior.enabled = true;
agent.enabled = true;
}
else
{
agent.enabled = false;
behavior.enabled = false;
}
Forgot to add error message: "SetDestination" can only be called on an active agent that has been placed on a Nav$$anonymous$$esh P.S and everything is baked but problem is still there
Answer by ShadoX · Jul 11, 2020 at 05:00 PM
I would suggest to just spawn the NPC with the NavMeshAgent disabled already and only enabling it once it hits the ground.
I've had the same issue and I just ended up checking for Collisions to see if the NPC has stopped falling and then just enabling the NavMeshAgent again, which should make it snap onto the NavMesh.
Generally the code you have should be working as long as you only enable the NavMeshAgent when it has touched the ground. It could be that the 0.1f difference is still too high for the NavMeshAgent to detect the NavMesh, so I would suggest to check for a collision instead, maybe ?