Introduction to Unity’s Nav Mesh System
A full month is over. Here’s a late report of yesterday, in which I have expanded my knowledge with Navigation in Unity.
Navigation is another perfect product of Unity that allows us to control the areas that the Player and AI can move on. By using Navigation, we can easily restrict the Player or the AI to move within a desired area, disallowing them from going outside of the specified area.
Before moving forward, I would highly suggest you to look to the Unity Manual on this subject, that can be found here. Additionally, I highly recommend you to look at the Unity Learn tutorials. You can find the one about Navigational Meshes (Nav Meshes) here.
In the most basic terms, Nav Mesh system allows us to create clever AI agents with minimum effort.
To start with, in order to open the Navigation window, simply click Window — AI — Navigation (for Unity 2020).
The Navigation window in Unity consists of 4 tabs namely “Agent — Areas — Bake and Object”.
The “term” agent is used for any game object that is going to move on the navigational mesh area. A Player character is an agent, whereas an enemy game object is also an agent.
Agent Types is used for defining different agents with different properties, we can simply add many agents by clicking the “+” icon next to it.
As you can observe, each Agent is represented with a Cylinder in the Navigation window. The values of the agents should be provided in a way so that the “body” of the agent fits within the cylinder. Meaning, the width of the character should fit into a cylinder with a Radius ‘R’ and height of ‘H’ provided in the property.
The ‘Step Height’ defines the maximum height that the agent can climb on at a single step; so that the character will not be able to step on an obstacle with a higher height at once. Likewise, ‘Max Slope’ defines the maximum amount of angle that the Agent can walk through.
Ever wondered how the AI follows you in a game? Not surprisingly, the game uses an algorithm in order to fulfil this demand. Areas field is used to define the costs for each path in the Navigational Mesh, that will be used by the algorithm that will move the Agents. By default, Unity defines 3 areas namely “Walkable”, “Not Walkable” and “Jump” (the areas that the Agent can jump on). We can add new area definitions according to our desire.
By using areas, we can easily differentiate between the capabilities of AI agents. For instance, we can block a certain type from climbing or using a certain route.
Nav Meshes are used in Unity to create paths for the character to walk on. The working procedure of Nav Meshes is pretty simple: it takes a meshed area as an input and devides the meshed area into multiple polygons by using “Bake”. In other words, baking process turns a meshed area into a Navigational Mesh area.
The costs in the Area tab are actually assigned after the baking process. To bake the desired area, simply select the surface you want to bake and click on “Bake”.
You will notice that the surface that we have baked will turn blue on the scene view, which represent the areas that the character and AI objects can move on.
An important note here is that Unity will take the Agent data on this tab into account while determining the areas that Agent can/can not move on.
This tab is for manipulating the objects that are on our Nav Mesh. We can define the corresponding area for an object from this tab.
And that’s actually all basic information about Navigation. Here’s a quick representation of the Nav Mesh system:
If you drag and drop a game object with a Nav Mesh Agent component to the Navigational Mesh area, you will realize that it will attach to the meshed area once we start the game and will only move on the areas that are specified with blue.