GLI: Framework 1

Challenge: Creating a Nav Mesh Agent for a Duck Hunt Game

Simon Truong
5 min readMar 14, 2023

--

In this challenge we will focus on our GLI Framework! To being we will start downloading our preset file that contains all of our assets and character controllers. The file I have gotten is from the GamedevHQ community and from the Filebase library and I highly recommend joining if you require a large library of 3D assets.

After extracting and installing the assets into your Unity project, our first focus is to setup the “Walkable” area for our AI. In order to do this, we need to select the correct folder containing all of the platforms that is required to be moved in.

Within the Navigation Menu (Windows > Navigations > AI) we are able to start baking our ground.

The area selected blue is the walkable path.

If the ground isn’t being highlighted blue and you are sure that it has been baked into a walkable path, this maybe be cause we have unchecked the Nav Mesh box on the bottom right in the scene view. Or it could be that “Gizmos” isn’t set to show. This can be turned turned back on just by clicking it.

Turning on the Nav Mesh and Gizmos will allow you to see the Baked pathways.

Next we will create two waypoints, one for the starting position and one for the end position. Just to organize the project a little better, I decided to create a new empty game object to house the waypoints.

Once the waypoints are in place, it is time to work on the actual AI agent that will be moving from waypoint to waypoint. The proper procedure in a prototype game such as this one is to make sure that the script is up and running first before we start working with 3D models and more complex assets. Therefore to just test the basic functions of the AI agent moving from one waypoint to another, we will just use a basic polygon first.

I created a capsule to represent our AI, and also attached an newly created C# script that will control the AI behavior and movement. I also added in the Nav Mesh Agent component to the AI.

Inside the AI movement script I am going to reference the Nav Mesh Agent component first.

Then I am going to serialized a list of waypoints (which we have created earlier) with another serialized int indicating the destination point (which will be our current go to waypoint)

Since in the near future we will be making the AI agent a prefab, it is required to preset the waypoint before hand. We do this by adding the waypoint list within the Start Method and creating a new list, all while getting the waypoints game objects.

we will need to get the waypoints transform position only.

Next we need to create the movement function. I start by creating an new method called “Calculate movement”, and within this method I make sure that the AI agent is NOT stopped, and if the agent’s remaining distance between the next waypoint is less than 1, we will move to the next path.

The “Go to Next Path” is also a new method which will actually set the destination. In the this method we will set the agent destination to the “next destination point” within our list of waypoints.
In the IF statement, if the destination point is equal to the total waypoint length (meaning it has reach the last waypoint) then we shall do nothing for now. Else we will add one value to our current destination point (meaning if our current destination is 1, then we will add another 1 to the destination value so it will become a 2. Once it becomes a higher value we will move towards that new waypoint)

Once that is setup, we will save and head back to unity where we can test out the script to see if the AI nav mesh agent will travel from one waypoint to another.

To test, just remember that the AI Nav Mesh Agent requires the waypoint transforms, so drag those to the required slots.

Tada! now we have moving objects within the scene!

In the next article we will focus on how to instantiate the AI nav mesh agent and also create an object pool so they can be tracked better.

--

--

Simon Truong

A Designer, an Illustrator and a massive tech geek aspiring to become a professional Unity Developer.