Unity Pathfinding Part 2: Nav Mesh Obstacles

In part 1 of this series, we looked at setting up the most critical components for pathfinding in Unity: setting up the navigation mesh in a scene, followed by creating a NavMeshAgent that will use the navigation mesh to travel around the world. At the end of the video, I had something that looked like the scene shown below: with a GameObject acting as a destination and the character able to move across a flat surface through use of a navigation mesh. Now this isn’t terribly interesting, given you typically want to create an environment with obstacles and road blocks that make the game world a lot more interesting.

Starting with the basic scene from part 1, with a NavMeshAgent and a destination it can reach.

So with that in mind, we’re going to take a quick look at how to place obstacles in the game world and ensure that not only does the navigation mesh recognise it as an obstacle but that your NavMeshAgent will also walk around it.

Creating a Nav Mesh Obstacle

So starting off with the scene from the first tutorial, I move the player and destination such that they need to walk towards each other. Then we add in an obstacle, courtesy of a standard 3D cube (in Hierarchy View: right-click >> 3D Object >> Cube).

Feel free to move the box scale accordingly in order to ensure it will definitely block the path that your NavMeshAgent is attempting to cross. In the diagrams and video, there is an additional material added to the obstacle such that it stands out from the floor.  Now with that in mind, simply press play and observe what happens next.

Adding an object to act as a wall does not work automatically.

Ultimately, the obstacle has zero effect on the NavMeshAgent – it simply walked through it. Now you may well think that this is given that the collision system is not on. If you add a RigidBody to the NPC object, then try it again, it will now collide with the wall, but the navigation system is not recognising that this block is effectively intersecting with the navigation volume on the plane.

To resolve this, we need the navmesh to recognise that this object must be considered an obstacle when the navmesh is baked. Even if you attempt to re-bake the navigation mesh at this point, it will still ignore the fact that there is a large wall sitting in the middle of it. To make that happen, we return to the Object tab of the Navigation pane and ensure that the Wall object is set as navigation static, as well as Not Walkable.  When we rebake the navigation mesh, the mesh will now be broken up around the wall and on-play the agent will walk around it.

By setting as static, the navigation mesh now recognises it is intersecting with the plane.

When you try again, you’ll note that the NPC now walks around the cube in order to get to is desired location and it will largely hug around the wall as close as it can.  Next, we’ll look at how to customise the space it consumes.

Changing Obstacle Size

Unity uses a specific class known as NavMeshObstacle that we add as a component to each object. Simply add that component to the cube, go into the Object tab of the navigation pane and make sure it is set as navigation static, then rebake the navigation mesh. You’ll note straight away that the navigation mesh has changed, given that there is a tiny amount of space around the obstacle, but also that the navigation mesh has been broken up into multiple plans in order to draw out to compensate for it being broken up by the obstacle.

You can play around with this such that the obstacle space on the navigation mesh is larger than the actual obstacle. So for example, if you return to the inspector on your wall you can customise both the shape and size of the NavMeshObstacle. So if you were to increase the x, y and z values on the size of the wall you can make it such that the obstacle takes up more space than usual. However, if you return to the navigation tab, by default this will not have any impact on the navigation mesh. Instead you need to ensure you have enabled the ‘Carve’ value, this actually carves a hole in the navigation mesh based on the bounds shown in the inspector fields. By default this is done when an item is stationary, but you can use this sort tool to have a moving object that continually cuts holes in the navigation mesh as it is moving, ensuring that any pathfinding does not attempt to walk through it as it is moving around. We can come back to look at that in a later video.

This slideshow requires JavaScript.


So in this second tutorial we’ve learned how to setup obstacles on our navigation mesh and have a the NavMeshAgent factor these when walking around the space. Feel free to play around with this environment and create some new obstacles, just ensure you set the parameters correctly and then re-build the navmesh after you change all this stuff. In part 3 we’re going to look at how to have your NavMeshAgent create patrols and wander around the map following preset locations. Allowing for you to create a more realistic guard behaviour moving around a fixed space.

Enjoying AI and Games? Please support me on Patreon!
Tommy Thompson Written by:

Tommy is the writer and producer of AI and Games. He's a senior lecturer in computer science and researcher in artificial intelligence with applications in video games. He's also an indie video game developer with Table Flip Games. Because y'know... fella's gotta keep himself busy.