Setting Up a NavMesh in Unity
Objective: To learn how to setup a NavMesh surface for the AI agents to move around in Unity.
This is a new article series that is going to look at some AI fundamentals in Unity such as — Baking a NavMesh surface, moving an AI agent on a NavMesh, creating a finite state machine for smarter AI and understanding what an Offmesh link is. Let’s get started with how to setup a NavMesh.
Getting the Scene Setup
I have used 2 cubes and rescaled them to create the floor you can see here:
I have then created a cube and duplicated it 6 times so that I can place them around the floor in different positions. These are going to act as the waypoints for the AI agents to move between. Making sure that these cubes have ‘IsTrigger’ ticked on their Box Collider component (so they don’t simply bump into the object, I want them to pass through).
To assist in cleaning up the Hierarchy, I have created an empty container and labelled it ‘Waypoints’, made sure all the transform positions have been zeroed out and dragged in all the waypoints.
I have also added 2 different color materials, so we can see the difference between the floor and the waypoints.
Baking the NavMesh
We need the ‘Navigation’ window for this so let’s go to:
Window → AI → Navigation. I have docked this next to the Inspector.
To get started let’s make sure we have our ‘Floor’ game objects selected in the Hierarchy, and then we want to be on the ‘Object’ tab in the Navigation window. Let’s tick ‘Navigation Static’ and change the ‘Navigation Area’ to ‘Walkable’.
From here, jump over to the ‘Bake’ tab and then simply select the ‘Bake’ button. Unity might take a short time to load, but you should now see the blue NavMesh surface on the floor:
You will also notice that the NavMesh surface goes under the cubes. If we didn’t want the AI agent to be able to walk into the cubes, I could turn on the waypoints ‘Navigation Static’ :
Agent Radius — This is going to determine how close the AI agent can get to a wall or a ledge. If you change this setting to something smaller:
E.g. 0.1, and then rebake, you will see that the NavMesh surface has gotten much closer to the edge.
Conversely, if don’t want the agent to be so close, increase the radius.
Agent Height — Determines the height of the AI agent and ultimately whether or not they would be able to move under an obstacle.
E.g. If the ceiling is too low to enter a new room, than the AI agent wouldn’t be able to move here.
Max Slope — This determines the degree of slope that the AI agent is able to walk up.
E.g. If this setting it set to 45, than any slopes that are greater than 45 degrees the agent won’t be able to walk up.
Step Height — The determines how high an AI agent is able to step up to other objects.
E.g. If the height between each stair is too much, the AI agent won’t be able to move up it.
“Moving Agents on a NavMesh in Unity”