Unity Development — Nav Mesh

In a 3D world, you can add the proper colliders to all objects in the scene to create realistic obstacles. Even with this done, an object using code to get from Point A to Point B will not know those obstacles are there. With Unity’s 3D Navigation system, we can quickly fix that.

For this example, we’ll have a 3D scene set up with furniture or other obstacles in a room. We’ll also need a floor to represent where the objects could ‘walk’. Highlight that object and open up a Navigation window by going to the Window tab -> AI -> Navigation.

In the Navigation window, you’ll be able to make adjustments to what area you believe the player or enemies should walk on. You can do this by adjusting variables in the ‘Bake’ tab, such as the radius of objects, the slope that is walkable, and more. Once you feel comfortable with these settings, hit the Bake button. Don’t worry, you can always makes changes later.

Your scene should have a blue outline to represent the movable spaces in the scene.

Now we need to tell our player object to abide by this newly baked Nav Mesh. We do that by adding a component to the player called a Nav Mesh Agent.

Here you can make more specific changes to this object. This way, not all objects who abide by the Nav Mesh are exactly the same. This will also add a new collider to the object that you can adjust for ‘Obstacle Avoidance.’

Now when you create enemies that go after the player, they’ll know how to traverse the space. If your player follows a pathfinding system, they too will know where they can move. As long as you have a Nav Mesh and agents on the proper objects, they will be smart enough to know where to go. Feels like your first real step into the world of AI.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store