NavMeshAgent with physics?

Hey all, I’m fairly new to unity and game dev. I’ve been dipping in and out of it for around a year just messing around on a very basic level. Started on a new idea and right now I’m wondering what you think would be the best idea for doing pathfinding on enemies that can also be knocked around with physics? I was hoping to use the navmeshagent component as I’ve played with it before and I like how it’s relatively easy to implement and is the kind of pathing I’m wanting, but I’ve found it doesn’t really play well with rigidbody/character controller, which I was initially using for physics and knocking things around.

I’ve tried implementing code from this thread ( Using a NavMeshAgent with a CharacterController ) and had some success but when game objects get launched it almost seems like the navmeshagent and the character controller get un-synced. The gizmo of the agent detaches from the transform of the object so the object starts walking the wrong way or thinks it’s at it’s destination when it isn’t.

I basically would just like to know if I’m going after the right solution or not. If using both these components is what I should be doing and it’s just a matter or learning how to get it working and hammering at the code or if there’s a better way to go about achieving this kind of behaviour. Cheers!

What you might try is just disable the NavMeshAgent for one frame. That would force it to re-initialize on the nav mesh at its current location. You will need to use a coroutine for that:

private IEnumerator resetNavMeshAgent() {
    this._agent.enabled = false;
    yield return null;
    this._agent.enabled = true;
}

Also, beware that you might have a top-level transform complicating things. If that’s the case, you will need to update that top-level transform and then reset the position of the character model.

Cheers for the reply, will try it out and see if I get any closer!

Hey, Theres actually a whole page on unity docs about mixing the agent with other components including physics! here you go: https://docs.unity3d.com/Manual/nav-MixingComponents.html

Cheers for the help, managed to get the kind of behaviour I was going for!

1 Like

I have a kinematic Rigidbody with colliders in children. My Navmeshagent can push physics objects but only so far with large speeds. At large speeds the objects phase through the colliders. How can I prevent this?

Physics can only handle a certain speed otherwise you need to use raycasting to prevent a phase trough. I would recommend giving them a mass or a proper drag to limit their speed.

I know the topic is a bit old, but I’ve found a surprisingly simple solution enabling a NavMeshAgent to have a dynamic Rigidbody.

I put my whole model (and colliders) inside a new empty object, and put all of the NavMesh components on the new parent object. Then I moved the rigidbody onto the child object.

Now the rigidbody can be fully dynamic and not effect the NavMeshAgent’s transform.

The trick was using NavMeshAgent.Warp() and doing a distance check between the rigidbody and the navmesh agent.

If the distance goes over a set threshold, I ‘unbalance’ the agent (disable it), and if it is less than I apply a small force towards the agent’s position (keeps it from sliding out of range on hills and terrain edges).

Then when the agent is moving slow enough (rigidbody velocity) and grounded (sweep test) I sync the agent to the rigidbody using Warp() and reenable it.

The final trick is caching the rigidbody’s position, before warping the agent, then moving the rigidbody back to the cached position. Helpfully the rigidbody.position is in world space so no transformation of coordinates is necessary.

Is this a performant solution? I don’t know yet, but it is working smoothly for my single agent. Soon I’ll be testing with hundreds of agents and I’ll learn more.

1 Like