In this lecture, I'll introduce you to a powerful concept using A.I. navigation called Navigation Meshes,
or NavMeshes. NavMeshes are a built-in Unity feature and are used to divide an environment up into navigatable
areas which the A* algorithm can then be used on for pathfinding. The method (originally called
a meta map) was developed for robotic applications
in the mid-1980s. It became more popular in the 2000s for use in games.
A navmesh divides a model into a series of polygons and assigns a cost to each.
This cost is considered in the pathfinding algorithm in determining the best path for an A.I. to take.
We call the A.I. using a navmesh an 'Agent'.
This is a generic term originating in traditional A.I. where words like NPC, Agent and Bot all mean the same
thing.
There are four tabs in the Unity navigation system for setting up a navmesh: Agents, Areas, Bake & Object.
The first defines an Agent.
An agent has 4 main properties that you can set to define the character size and movement abilities.
These are used by a navmesh to determine where a character can and can't go in the environment.
NOTE: They are not based on the properties you might set in the physics system, nor are they based on
the model size you are using,
so set these carefully. The size of an agent is defined by a cylinder that the character would fit within.
It has a radius for determining how wide your character is and a height. It is generally assumed the character
will be navigating across a flat terrain, but in the case of slopes and steps, you need to declare the gradient
of slopes accessible by the character, as well as the height of stairs that it can access. The Agents
tab will also let you define different sized agents by adding more than one type.
This allows you to have many types of agents, all with differing sizes and navigation abilities.
The Areas tab is where you can define different costs for different parts of the navmesh.
The cost is used by the A* algorithm to calculate the best path across the mesh from one location
to another.
There are 3 built-in types that are used to set different parts of a mesh to walkable, not walkable
and jumpable. To these,
you can add your own.
Any area type can be included or excluded in an agent's pathfinding search across the terrain.
This makes it possible to have some characters unable to traverse water or walk through doorways.
The Bake tab turns your meshes into a navmesh.
It does this by dividing the mesh into numerous polygons and determines where the agent should be able
to travel using the Baked Agent Size.
When you create a navmesh in the Bake tab, it uses this agent size and not the sizes you set up in the
Agents tab.
By default, the 'Humanoid' agent type is the only agent type recognized by the navmesh and even though you
can define multiple agent types in the Agent tab, you can only use Humanoid in the set up given to you
via Unity.
Having said that, there are beta components available that'll allow you to set up different navmeshes
for different agent types and we will look at this in later workshops. Other bake settings include
being able to generate 'Off Mesh Links'.
These define how far across a gap in the mesh an agent can fall or jump.
The detail of the navmesh can be set using the 'Manual Voxel Size' setting. Voxels are a data structure used
to divide the world up, before the navmesh is constructed.
The bigger the voxel size, the less accurate the navmesh as bigger world divisions
will miss some of the detail in the mesh. This value is usually set to 3 per Agent Radius. You are aiming
for values around 2 and 4. The 'Min Region Area' allows you to move sections of the mesh that
were calculated as navigatable but are too small to be of any use and you don't really want them included
in the pathfinding. By increasing the minimum region area and rebaking,
you can remove these small areas. When a navmesh is generated for a bunch of meshes,
it averages out the height and can do things like turning stairs into a slope.
If this is not the effect you're after, then you can turn on the 'Height Mesh' box before baking.
This will include height values into the mesh.
It will also take up more memory and baking time,
so don't be too generous with its use.
The last tab (the Object tab) is where you can align specific area types to different parts of your mesh.
These are the areas defined in the Areas tab.
This tab also allows you to specify which parts of your scene you want to create a navmesh for, as
well as generating off mesh links that allow you to transition from one navmesh to another. Navmeshes
are a powerful system to get your NPCs moving with very little effort on your behalf.
With them, you can create all types of scenarios from patrols and crowds, to traffic and flocks. In this section
we will examine some of the fundamental elements that make navmeshes work and create some examples
that really demonstrate their power.