- Home /
Make navmesh agent fall from ledge
I'm looking for a way to make my NPCs fall off a ledge (after being kicked by the player). They use NavMesh Agents for pathfinding.
I tried turning off the navmesh agent while it is being pushed, adding force to the attached rigidbody and re-enabling it a few seconds later. If they land on a surface without navmesh (like an edge) then they warp to the navmesh point directly below and it looks odd or sometimes get stuck in in-accessible areas.
Is there a better way to do this?
Answer by Harinezumi · Apr 17, 2018 at 09:18 AM
I think the first step should be to detect if the agent landed outside a navmesh. Maybe NavMesh.SamplePosition() can help with that, or maybe NavMeshAgent.FindClosestEdge() (the navmesh API is really bad at handling these situations).
Next, how would you like to handle the situation when the agent has landed outside? I assume you don't want them to return at the closest point on the navmesh, because that is the current behaviour. What if they were killed, e.g. fallen off a cliff?
This is applicable only if they are still alive after falling as I disable the navmesh agent once they are dead. Using SamplePosition on the Nav$$anonymous$$esh looks very good. Thanks!
Nav$$anonymous$$eshHit hit;
var sampleResult = Nav$$anonymous$$esh.SamplePosition(transform.position, out hit, navemeshAgent.radius, Nav$$anonymous$$esh.AllAreas);
if (sampleResult)
{
transform.position = hit.position;
navemeshAgent.enabled = true;
}
else
{
Debug.Log("navmesh agent landed outside navmesh, cannot get back in");
}
Did it work? Then I will convert the comment into an answer, so that others can find it easier.