GLI: Framework 1

Working with AI States

Simon Truong
5 min readApr 17, 2023

When working with AI agents, especially when you want your AI to perform multiple functions it is best to create different states. To introduce different states for your AI we would need to work with Enums and switch cases. With Enums you will be setting up all the different scenarios which you would like the AI to preform. Like the example below, I have setup my AI to have the following states; Walking, Hiding, and Death.

An Example of using Enum to create different AI states

In this challenge we will be setting up our Enums AI states and also be adding in functionality to each state.

Since this will be a multipart challenge we will be dividing the article into several parts. In the first part we will focus on Creating and working with the “Run” state for the AI.

Run State:

Since we have already setup the AI agent to activate seek out the last waypoint which is the exit, we already have the basic movement down. The only function we would like to add in is selecting random barriers or barricades to hide behind to avoid the player detection.

In order to set that up we will need to revise our current movement script. First we will need to use our Enum within a new Switch case function. In the Update method we will create a switch case regarding our “current AI State” which will rotate based on set conditions. We will set our first and current AI state to be “Walking”.

We will apply our “Calculate Movement method” inside the Walking AI state. Next we will need to re-adjust the waypoint system by adding more waypoints than just the start and end points. We will need to strategically place new waypoints within the scene and allow the AI to be able to approach them.

An Image of different waypoints set behind barriers.

Depending on how many waypoints you add, you will need to identify each waypoint for the spawned AI agent to register. I have a total of 7 different waypoints that are set behind barriers, so I will need 7 different waypoint registry.

Hiding State:

The next state which we will be working on is the Hiding state, this means that the AI agent will be able to hide behind an barrier for a random set of seconds before moving forward to the next waypoint.

To setup the hiding state, we can follow the same setup as we did for the Walking state by using the switch statement.

The local method “Calculating Hiding” is going to be an method which contains an Coroutine, and this coroutine will be the timer which the AI agent will be hiding behind the barrier.

Inside the Coroutine, we will tell the AI agent to stop moving, and wait for a random set of seconds before turning back on the AI agent movement. Then we will set the AI state back to “Walking”. Once set back to walking state, the AI agent will resume its travel to the next waypoint, and will trigger.

To Trigger the Hiding state, we will need to add the Hiding method within the Calculate Movement method, since we want the AI agent to trigger an hiding state whenever it reaches an barrier waypoint.

In the hiding method, we will set the AI current state to hiding and also set the bool for “is hiding” to true to allow the triggering of the coroutine to happen.

Death State:

In the death state, we will need to do several things, first is gain access to the animator and trigger an death animation, then we will need to add 50 digital points to the total collective. Since we haven’t set up the function to destroy the AI agents just yet, we can focus on setting up the point system.

For now, we can do a minor setup like shown below, where we will have the AI agent to be set to false, therefore making the AI agent disappear. Then we will add 50 points to our total points collective.

Since the point system isn’t scripted yet, we can start by creating some new methods within the Game Manager script since the Game Manager is an singleton we can gain access to any methods via instances.

Inside the Game Manger script we will create two new local variables, one called points and the other called total points.

Then we are going to create two new methods, one public and one local. the Public method is what the AI Movement script is going to gain access to.

This will send the point amount over from the AI movement script to the Game Manager script.

The local method is going to be the total collected amount of points.

This way, the total can be summed up and we can use this total for later UI use.

Overall we now have setup 3 different AI states along with their proper functions except the Death state since that requires ray casting from the gun, and also the animator to trigger the death animation. All these will be further covered in the next articles.

--

--

Simon Truong

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