Unity : AI [Navmesh implementation (Part I)]

Matteo Lo Piccolo
4 min readJan 13, 2022

In this article we talk about something incredibly useful and really fun : the AI with Navmesh implementation.

This is a series of articles on how Navmesh works with a (basic) AI implementation.
Let’s start from here up to what is called FSM or FINITE STATE MACHINE.

Let’s create a scene and let’s get started!

First we need a Floor, I use a classic Cube with scale 10, 1, 10

Then I create another Cube that we use as point and create a material to assign it. (the color is absolutely not important, choose what you want).
Then, in the BoxCollider toggle the Trigger so we can walk through it and make it a prefab.

Now, to set up the scene, we create four “Point” and we drop them in the four corner.
So now we have a sort of “path”.

Now, the last thing we have to do for create a good pathwalk, is to duplicate the Floor, and move it to the right of the first Floor and snap it to it

NOTE : to snap it without problem and in easy way, hold down V Button e move the mouse near the corner.
When appear the icon, just take it and move it.
It snap it to the other piece in automatic.

After we make that, we change the scale of Z axis to 6, and we create another two Point, and we move them one here and one here (see image below).

This is my scene

A good thing to do is create an empty GameObject to “contain” all the points.

Now that we have set the scene, we can start with the real fun!

We start with documentation :
https://docs.unity3d.com/Manual/nav-BuildingNavMesh.html

IMPORTANT NOTE : the Navmesh system is the equivalent as A* (or A Star) algorithm, better know as PATHFINDING.

This is what the Nav Mesh System do

Thanks to the documentation we have these important informations about Navmesh

While it may seem intimidating, using the Navmash system isn’t that difficult.
From now until the end, we implement this system step by step, so we understand how it works.

The first thing to do is to create the Navmesh, so we can tell to the AI which is the surface where it can move.

To do this, we select both Floor, then we go to
Windows -> AI -> Navigation

It opens Navigation menu near Inspector, inside we need to select static, and in the Navigation Area we select Walkable.

To apply the Navmash, in the Navigation menu we select Bake, and then click on Bake

After we bake the Floor, we see the blue zone around it.
If we look better, we see that the blue is not so near at the end of the floor.
But we can change it in the options (Agent Radius).

If we change it from 0.5 to 0,1 and bake it again this is the result

The blue zone is now really near to the borders of the floor.

I take it back to the 0.5f, and in the next articles we place the agent and we see how make it move on this surface!

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!