Knockback enemy using NavAgent3d

Godot Version

4.2

Question

I’m currently working in a 3D game using the NavigationAgent for the AI to follow my player. I want to be able to hit and knockback the enemy, without the possibility of it leaving the NavigationRegion3D. Creating invisible walls would be a huge pain, so I thought about using the NavAgent to calculate a direction to go in towards the knockback, but can’t figure out how to do it since they still fall off. Any help would be appreciated.

Frankly because it is the physics engine (and in part your own movement code) doing all that adding “invisible walls” would be the right solution.

The navigation system does not touch both physics and movement so involving it “trying to intervene” will be at best a bandaid to the problem.

The NavigationAgent path will always be on the navigation mesh so if you “stay” with those positions you are automatically on the navigation mesh. You can query occasionally the closest position on navigation mesh with the NavigationServer.map_get_closest_point() function. If that position is further away than your agent_radius + some margin your agent was moved by an external force too far away from the navigation mesh, e.g. physics, and should be snapped back. The reason why you only should call this every few frames at best is because it is a costly function that searches all navigation map polygons. It can cause performance problems on larger, more complex maps.