How To Create And Utilize A NavMesh In Unity
A “Nav Mesh” or “Navigational Mesh” is a path that can be created along object surfaces that utilize mesh colliders to customize which areas are considered walkable by objects utilizing unities navigational system.
The process of creating a Nav Mesh in your level is known as “Nav Mesh Baking”. This process collects the “Render Meshes” and “Terrains” of your game objects and compiles them to create a navigational mesh that approximates the walkable surface of a level.
To start, let’s add the “Navigation” window to your Unity editor!
Drop down the “Window” tab on the Unity toolbar located at the top left of your editor. Then select “AI>Navigation”.
Now, in order to create a nav mesh you must first create or select an object that utilizes a mesh collider. For this example, I will be creating a flat “Plane” game object by right-clicking within the hierarchy and selecting “3D Object>Plane”. Notice the Plane comes with a “Mesh Collider” attached to it by default.
In order for an object to have its mesh collider baked into a nav mesh the object MUST be marked as “Static” in the inspector.
Next, all you have to do is highlight the Plane object in your hierarchy and then open up the “Navigation” window, switch to the “Bake” tab, and select Bake.
Congratulations, you have just successfully created a Navigational Mesh. The area on the Plane that is now highlighted in “Blue” is where the nav mesh considers walkable terrain!
Now how do you make an object capable of interacting with the Nav Mesh?
To do this, you must add the “Nav Mesh Agent” component to the object that you are wanting to be regulated. A “Nav Mesh Agent” is what allows an object to receive instructions about what it can walk on as well as processing a number of other things such as Steering, Obstacle Avoidance, and Pathfinding.