Getting NavMeshAgents to avoid obstacles effectively

I’m making a tower defense that uses a navmesh for AI movement. I want the player to be able to place objects down and have the AI avoid them. Right now, the NavMeshObstacle doesn’t really do much besides stopping the AI from running into it. So basically I have a problem where is there is a cone of obstacles, the AI will run straight into it and stay there.

This is my crude drawing of what is happening. The AI starts at the green and runs into the black obstacles along the black path, stopping at the red point. I want it to be able to follow the green lines instead of getting stuck there. This also happens when the obstacles are overlapping, so it tries to get through even with no space at all.

Is there a way to get the AI to actually avoid the obstacles instead of running into them?

Unfortunately Unity’s Navmesh doesn’t update dynamically. What you actually want is that dynamic obstacles are included in the pathfinding itself. However this doesn’t work. Unity only has a very simple “avoidance” behaviour which will try to slightly go left or right to avoid the obstacle, however if the calculated path goes straight through a group of obstacles it fails totally.

I heven’t used it myself but a workmate has used Unity’s Navemesh and come across the same problem. Our solution was to use a third party Navmesh framework which allows even recreation of the Navmesh at runtime as well as dynamically updating the Navmesh. The framework isn’t as optimised as Unity’s when it comes to simplify the navmesh. It just uses a grid mesh and executes an A* pathfinder on it. But the project target is mobile (iPad, Android) and it seems to work pretty well.

Unfortunately i can’t remember where we found it, but as far as i remember it was actually an opensource framework.

A quick google gives me this and this but i guess there are a lot others out there. I think we used the one in the second link, but i don’t used it myself, so i’m not sure :wink: