Working with Unity Nav Mesh Ai
A Nav Mesh is a designated mesh in your Unity scene, which specifies navigable areas in your environment, including areas where characters can walk, as well as obstacles. This is useful for scenarios which incorporate pathfinding and AI-controlled navigation.
Setting up a Scene:
To demonstrate the Nav Mesh system we are going to built a small controlled environment that a object can run around in. In Unity we are going to setup two cubes that are going to act as our Floor. Then we are going to place them like the example 1 shown below. Following that we are going to setup waypoints, which are the green cylinders in each corner like so. We are also going to categorize each waypoint with a letter and also organize them within a folder (aka empty game object) called waypoints.
Finally we are going to add in a red sphere that will act as our future Ai component.
To setup the Nav Mesh we first need to take out the tab that is responsible for the function. Under Windows > AI > Navigation we will open up the Navigations tab.
Once the tab is open, we are going to select the “Object” Tab. Select all the Floor like objects that you want the Ai to be able to walk on. Once you do, you are able to check the “Navigation Static” checkbox and select the Navigation Area as “Walkable”.
Once you select the Floor Objects to be part of the Nav Mesh construct, you will see that floor turn Cyan. This is the indicator that the floor is now “walkable”.
You will noticed that the blue indicator doesn’t reach all the way out to the edge of the floors, but there is a way to adjust that value. In the Bake tab you are able to adjust the “Agent Radius”. By Default it should 0.5, but if you shrink the number the radius would get larger. There is also various other settings which can adjust the outcome of the Nav Mesh.
Nav Mesh Agent:
Next we are going work with the Ai sphere that we setup. In order to identify the sphere as an Ai object, we will need to attached an Nav Mesh Agent component.
The Nav Mesh Agent will give the Ai a cylinder collider that will track the Nav Mesh Floor. With the Nav Mesh Agent you will be able to adjust the Offset to make the Ai Sphere stick closer to the floor.
The Nav Mesh Agent can also adjust other various controls such as Steering and how the Ai approaches waypoints, movement speed, Stopping Distance etc.
In the next article we will focus on how to use the Nav Mesh Agent to script the Ai to move along our controlled environment using the Waypoints.