Unity AI

Unity AI

Artificial Intelligence, also known as AI, is something that you’ll see in every video game that you play. First-person shooter, real-time strategy, simulation, role playing games, sports, puzzles, and so on, all have various forms of AI in both large and small systems. In this article, we’ll be going over several topics that involve creating AI, including techniques, actions, path-finding, animations, and the AI manager. Then, finally, we’ll put it all together to create an AI package of our own.

AI techniques

There are two very common techniques used to create AI: the finite state machine and the behavior tree. Depending on the game that you are making and the complexity of the AI that you want, the technique you use will vary. In this article, we’ll utilize both the techniques in our AI script to maximize the potential of our AI.

Finite state machines

Finite state machines are one of the most common AI systems used throughout computer programming. To define the term itself, a finite state machine breaks down to a system, which controls an object that has a limited number of states to exist in. Some real-world examples of a finite state machine are traffic lights, television, and a computer. Let’s look at an example of a computer finite state machine to get a better understanding.

A computer can be in various states. To keep it simple, we will list three main states. These states are On, Off, and Active. The Off state is when the computer does not have power running it, the On state is when the computer does have power running it, and the Active state is when someone is using the computer. Let’s take a further look into our computer finite state machine and explore the functions of each of its states:

State Function
On
  • Can be used by anyone
  • Can turn off the computer
Off
  • Can turn on the computer
  • Computer parts can be operated on
Active
  • Can access the Internet and various programs
  • Can communicate with other devices
  • Can turn off the computer

Each state has its own functions. Some of the functions of each state affect each other, while some do not. The functions that do affect each other are the functions that control what state the finite state machine is in. If you press the power button on your computer, it will turn on and change the state of your computer to On. While the state of your computer is On, you can use the Internet and possibly some other programs, or communicate to other devices such as a router or printer. Doing so will change the state of your computer to Active. When you are using the computer, you can also turn off the computer by its software or by pressing the power button, therefore changing the state to Off.

In video games, you can use a finite state machine to create AI with a simple logic. You can also combine finite state machines with other types of AI systems to create a unique and perhaps more complex AI system. In this article, we will be using finite state machines as well as what is known as a behavior tree.

The behavior tree form of the AI system

A behavior tree is another kind of AI system that works in a very similar way to finite state machines. Actually, behavior trees are made up of finite state machines that work in a hierarchical system. This system of hierarchy gives us great control over an individual, and perhaps many finite state systems within the behavior tree, allowing us to have a complex AI system.

Taking a look back at the table explaining a finite state machine, a behavior tree works the same way. Instead of states, you have behaviors, and in place of the state functions, you have various finite state machines that determine what is done while the AI is in a specific behavior. Let’s take a look at the behavior tree that we will be using in this article to create our AI:

 

On the left-hand side, we have four behaviors: Idle, Guard, Combat, and Flee. To the right are the finite state machines that make up each of the behaviors. Idle and Flee only have one finite state machine, while Guard and Combat have multiple. Within the Combat behavior, two of its finite state machines even have a couple of their own finite state machines.

As you can see, this hierarchy-based system of finite state machines allows us to use a basic form of logic to create an even more complex AI system. At the same time, we are also getting a lot of control by separating our AI into various behaviors. Each behavior will run its own silo of code, oblivious to the other behaviors. The only time we want a behavior to notice another behavior is either when an internal or external action occurs that forces the behavior of our AI to change.

Combining the techniques

In this article, we will take both of the AI techniques and combine them to create a great AI package. Our behavior tree will utilize finite state machines to run the individual behaviors, creating a unique and complex AI system. This AI package can be used for an enemy AI as well as a friendly AI.

Let’s start scripting!

Now, let’s begin scripting our AI! To start off, create a new C# file and name it AI_Agent. Upon opening it, delete any functions within the main class, leaving it empty. Just after the using statements, add this enum to the script:

public enum Behaviors {Idle, Guard, Combat, Flee};

This enum will be used throughout our script to determine what behavior our AI is in. Now let’s add it to our class. It is time to declare our first variable:

public Behaviors aiBehaviors = Behaviors.Idle;

This variable, aiBehaviors, will be the deciding factor of what our AI does. Its main purpose is to have its value checked and changed when needed. Let’s create our first function, which will utilize one of this variable’s purposes:

void RunBehaviors()
{
switch(aiBehaviors)
   {
    case Behaviors.Idle:
       RunIdleNode();
       break;
    case Behaviors.Guard:
       RunGuardNode();
       break;
    case Behaviors.Combat:
       RunCombatNode();
       break;
    case Behaviors.Flee:
       RunFleeNode();
       break;
   }
}

What this function will do is check the value of our aiBehaviors variable in a switch statement. Depending on what the value is, it will then call a function to be used within that behavior. This function is actually going to be a finite state machine, which will decide what that behavior does at that point. Now, let’s add another function to our script, which will allow us to change the behavior of our AI:

void ChangeBehavior(Behaviors newBehavior)
{
    aiBehaviors = newBehavior;

    RunBehaviors();
}

As you can see, this function works very similarly to the RunBehaviors function. When this function is called, it will take a new behaviors variable and assign its value to aiBehaviors. By doing this, we changed the behavior of our AI. Now let’s add the final step to running our behaviors; for now, they will be empty functions that act as placeholders for our internal and external actions. Add these functions to the script:

void RunIdleNode()
{

}

void RunGuardNode()
{

}
void RunCombatNode()
{

}
void RunFleeNode()
{

}

Each of these functions will run the finite state machines that make up the behaviors. These functions are essentially a middleman between the behavior and the behavior’s action. Using these functions is the beginning of having more control over our behaviors, something that can’t be done with a simple finite state machine.

Internal and external actions

The actions of a finite state machine can be broken up into internal and external actions. Separating the actions into the two categories makes it easier to define what our AI does in any given situation. The separation is helpful in the planning phase of creating AI, but it can also help in the scripting part as well, since you will know what will and will not be called by other classes and GameObjects. Another way this separation is beneficial is that it eases the work of multiple programmers working on the same AI; each programmer could work on separate parts of the AI without as many conflicts.

External actions

External actions are functions and activities that are activated when objects outside of the AI object act upon the AI object. Some examples of external actions include being hit by a player, having a spell being cast upon the player, falling from heights, losing the game by an external condition, communicating with external objects, and so on.

The external actions that we will be using for our AI are:

  • Changing its health
  • Raising a stat
  • Lowering a stat
  • Killing the AI

Internal actions

Internal actions are the functions and activities that the AI runs within itself. Examples of these are patrolling a set path, attacking a player, running away from the player, using items, and so on. These are all actions that the AI will choose to do depending on a number of conditions.

The internal actions that we will be using for our AI are:

  • Patrolling a path
  • Attacking a player
  • Fleeing from a player
  • Searching for a player

Scripting the actions

It’s time to add some internal and external actions to the script. First, be sure to add the using statement to the top of your script with the other using statements:

using System.Collections.Generic;

Now, let’s add some variables that will allow us to use the actions:

public bool isSuspicious = false;
public bool isInRange = false;
public bool FightsRanged = false;
public List> Stats = new List>();
public GameObject Projectile;

The first three of our new variables are conditions to be used in finite state machines to determine what function should be called. Next, we have a list of the KeyValuePair variables, which will hold the stats of our AI GameObject. The last variable is a GameObject, which is what we will use as a projectile for ranged attacks.

Remember the empty middleman functions that we previously created? Now with these new variables, we will be adding some code to each of them. Add this code so that the empty functions are now filled:

void RunIdleNode()
{
    Idle();
}

void RunGuardNode()
{
    Guard();
}
void RunCombatNode()
{
    if(FightsRanged)
       RangedAttack();
    else
       MeleeAttack();
}
void RunFleeNode()
{
    Flee();
}

Two of the three boolean variables we just created are being used as conditionals to call different functions, effectively creating finite state machines. Next, we will be adding the rest of our actions; these are what is being called by the middleman functions. Some of these functions will be empty placeholders, but will be filled later on in the article:

void Idle()
{
}

void Guard()
{
    if(isSuspicious)
    {
       SearchForTarget();
    }
    else
    {
       Patrol();
    }
}
void Combat()
{
    if(isInRange)
    {
       if(FightsRanged)
       {
         RangedAttack();
       }
       else
       {
         MeleeAttack();
       }
    }
    else
    {
       SearchForTarget();
    }
}
void Flee()
{
}

void SearchForTarget()
{
} 
void Patrol()
{
}

void RangedAttack()
{
    GameObject newProjectile;
    newProjectile = Instantiate(Projectile, transform.position, Quaternion.identity) as GameObject;
}

void MeleeAttack()
{
}

In the Guard function, we check to see whether the AI notices the player or not. If it does, then it will proceed to search for the player; if not, then it will continue to patrol along its path. In the Combat function, we first check to see whether the player is within the attacking range; if not, then the AI searches again. If the player is within the attacking range, we check to see whether the AI prefers attacking up close or far away.

For ranged attacks, we first create a new, temporary GameObject variable. Then, we set it to an instantiated clone of our Projectile GameObject. From here, the projectile will run its own scripts to determine what it does. This is how we allow our AI to attack the player from a distance.

To finish off our actions, we have two more functions to add. The first one will be to change the health of the AI, which is as follows:

 

void ChangeHealth(int Amount)
{
if(Amount {
   if(!isSuspicious)
   {
     isSuspicious = true;
     ChangeBehavior(Behaviors.Guard);
   }
}
for(int i = 0; i {
   if(Stats[i].Key == "Health")
   {
     int tempValue = Stats[i].Value;
     Stats[i] = new KeyValuePair(Stats[i].Key, tempValue += Amount);
     if(Stats[i].Value      {
       Destroy(gameObject);
     }
     else if(Stats[i].Value      {
       isSuspicious = false;
       ChangeBehavior(Behaviors.Flee);
     }
     break;
   }
}
}

 

This function takes an int variable, which is the amount by which we want to change the health of the player. The first thing we do is check to see if the amount is negative; if it is, then we make our AI suspicious and change the behavior accordingly. Next, we search for the health stat in our list and set its value to a new value that is affected by the Amount variable. We then check if the AI’s health is below zero to kill it; if not, then we also check if its health is below 25. If the health is that low, we make our AI flee from the player.

To finish off our actions, we have one last function to add. It will allow us to affect a specific stat of the AI. These modifications will either add to or subtract from a stat. The modifications can be permanent or restored anytime. For the following instance, the modifications will be permanent:

void ModifyStat(string Stat, int Amount)
{
for(int i = 0; i {
   if(Stats[i].Key == Stat)
   {
     int tempValue = Stats[i].Value;
     Stats[i] = new KeyValuePair(Stats[i].Key, tempValue += Amount);
     break;
   }
}
if(Amount {
   if(!isSuspicious)
   {
     isSuspicious = true;
     ChangeBehavior(Behaviors.Guard);
   }
}
}

This function takes a string and an integer. The string is used to search for the specific stat that we want to affect and the integer is how much we want to affect that stat by. It works in a very similar way to how the ChangeHealth function works, except that we first search for a specific stat. We also check to see if the amount is negative. This time, if it is negative, we change our AI behavior to Guard. This seems to be an appropriate response for the AI after being hit by something that negated one of its stats!

Pathfinding

Pathfinding is how the AI will maneuver around the level. For our AI package, we will be using two different kinds of pathfinding, NavMesh and waypoints. The waypoint system is a common approach to create paths for AI to move around the game level. To allow our AI to move through our level in an intelligent manner, we will use Unity’s NavMesh component.

Creating paths using the waypoint system

Using waypoints to create paths is a common practice in game design, and it’s simple too. To sum it up, you place objects or set locations around the game world; these are your waypoints. In the code, you will place all of your waypoints that you created in a container of some kind, such as a list or an array. Then, starting at the first waypoint, you tell the AI to move to the next waypoint. Once that waypoint has been reached, you send the AI off to another one, ultimately creating a system that iterates through all of the waypoints, allowing the AI to move around the game world through the set paths. Although using the waypoint system will grant our AI movement in the world, at this point, it doesn’t know how to avoid obstacles that it may come across. That is when you need to implement some sort of mesh navigation system so that the AI won’t get stuck anywhere.

Unity’s NavMesh system

The next step in creating AI pathfinding is to create a way for our AI to navigate through the game world intelligently, meaning that it does not get stuck anywhere. In just about every game out there that has a 3D-based AI, the world it inhabits has all sorts of obstacles. These obstacles could be plants, stairs, ramps, boxes, holes, and so on. To get our AI to avoid these obstacles, we will use Unity’s NavMesh system, which is built into Unity itself.

Setting up the environment

Before we can start creating our pathfinding system, we need to create a level for our AI to move around in. To do this, I am just using Unity primitive models such as cubes and capsules. For the floor, create a cube, stretch it out, and squish it to make a rectangle. From there, clone it several times so that you have a large floor made up of cubes.

Next, delete a bunch of the cubes and move some others around. This will create holes in our floor, which will be used and tested when we implement the NavMesh system. To make the floor easy to see, I’ve created a material in green and assigned it to the floor cubes.

After this, create a few more cubes, make one really long and one shorter than the previous one but thicker, and the last one will be used as a ramp. I’ve created an intersection of the really long cube and the thick cube. Then, place the ramp towards the end of the thick cube, giving access to the top of the cubes.

Our final step in creating our test environment is to add a few waypoints for our AI. For testing purposes, create five waypoints in this manner. Place one in each corner of the level and one in the middle. For the actual waypoints, use the capsule primitive. For each waypoint, add a rigid body component. Name the waypoints as Waypoint1, Waypoint2, Waypoint3, and so on. The name is not all that important for our code; it just makes it easier to distinguish between waypoints in the inspector. Here’s what I made for my level:

 Creating the NavMesh

Now, we will create the navigation mesh for our scene. The first thing we will do is select all of the floor cubes. In the menu tab in Unity, click on the Window option, and then click on the Navigation option at the bottom of the dropdown; this will open up the Navigation window. This is what you should be seeing right now:

By default, the OffMeshLink Generation option is not checked; be sure to check it. What this does is create links at the edges of the mesh allowing it to communicate with any other OffMeshLink nearby, creating a singular mesh. This is a handy tool since game levels typically use more than one mesh as a floor.

The Scene filter will just show specific objects within the hierarchy view list. Selecting all the objects will show all of your GameObjects. Selecting mesh renderers will only show GameObjects that have the mesh renderer component. Then, finally, if you select terrains, only terrains will be shown in the Hierarchy view list.

The Navigation Layer dropdown will allow you to set the area as either walkable, not walkable, or jump accessible. Walkable areas typically refer to floors, ramps, and so on. Non-walkable areas refer to walls, rocks, and other various obstacles.

Next, click on the Bake tab next to the Object tab. You should see information that looks like this:

For this article, I am leaving all the values at their defaults. The Radius property is used to determine how close to the walls the navigation mesh will exist. Height determines how much vertical space is needed for the AI agent to be able to walk on the navigation mesh. Max Slope is the maximum angle that the AI is allowed to travel on for ramps, hills, and so on. The Step Height property is used to determine how high the AI can step up onto surfaces higher than the ground level.

For Generated Off Mesh Links, the properties are very similar to each other. The Drop Height value is the maximum amount of space the AI can intelligently drop down to another part of the navigation mesh. Jump Distance is the opposite of Height; it determines how high the AI can jump up to another part of the navigation mesh.

The Advanced options are to be used when you have a better understanding of the NavMesh component and want a little more out of it. Here, you can further tweak the accuracy of the NavMesh as well as create Height Mesh to coincide with the navigation mesh.

Now that you know all the basics of the Unity NavMesh, let’s go ahead and create our navigation mesh. At the bottom-right corner of the Navigation tab in the Inspector window, you should see two buttons: one that says Clear and the other that says Bake. Click on the Bake button now to create your new navigation mesh.

Select the ramp and the thick cube that we created earlier. In the Navigation window, make sure that the OffMeshLink Generation option is not checked, and that Navigation Layer is set to Default. If the ramp and the thick cube are not selected, reselect the floor cubes so that you have the floors, ramp, and thick wall selected. Bake the navigation mesh again to create a new one. This is what my scene looks like now with the navigation mesh:

 

You should be able to see the newly generated navigation mesh overlaying the underlying mesh. This is what was created using the default Bake properties. Changing the Bake properties will give you different results, which will come down to what kind of navigation mesh you want the AI to use. Now that we have a navigation mesh, let’s create the code for our AI to utilize. First, we will code the waypoint system, and then we will code what is needed for the NavMesh system.

Adding our variables

To start our navigation system, we will need to add a few variables first. Place these with the rest of our variables:

public Transform[] Waypoints;
public int curWaypoint = 0;
bool ReversePath = false;
NavMeshAgent navAgent;
Vector3 Destination;
float Distance;

The first variable is an array of Transforms; this is what we will use to hold our waypoints. Next, we have an integer that is used to iterate through our Transform array. We have a bool variable, which will decide how we should navigate through the waypoints.

The next three variables are more oriented towards our navigation mesh that we created earlier. The NavMeshAgent object is what we will reference when we want to interact with the navigation mesh. The destination will be the location that we want the AI to move towards. The distance is what we will use to check how far away we are from that location.

Scripting the navigation functions

Previously, we created many empty functions; some of these are dependent on pathfinding. Let’s start with the Flee function. Add this code to replace the empty function:

void Flee()
{
for(int fleePoint = 0; fleePoint {
   Distance = Vector3.Distance(gameObject.transform.position, Waypoints[fleePoint].position);
   if(Distance > 10.00f)
   {
     Destination = Waypoints[curWaypoint].position;
     navAgent.SetDestination(Destination);
     break;
   }
   else if(Distance    {
     ChangeBehavior(Behaviors.Idle);
   }
}
}

What this for loop does is pick a waypoint that has Distance of more than 10. If it does, then we set the Destination value to the current waypoint and move the AI accordingly. If the distance from the current waypoint is less than 2, we change the behavior to Idle.

The next function that we will adjust is the SearchForTarget function. Add the following code to it, replacing its previous emptiness:

void SearchForTarget()
{
Destination = GameObject.FindGameObjectWithTag("Player").transform.position;
navAgent.SetDestination(Destination);
Distance = Vector3.Distance(gameObject.transform.position, Destination);
if(Distance    ChangeBehavior(Behaviors.Combat);}

This function will now be able to search for a target, the Player target to be more specific. We set Destination to the player’s current position, and then move the AI towards the player. When Distance is less than 10, we set the AI behavior to Combat.

Now that our AI can run from the player as well as chase them down, let’s utilize the waypoints and create paths for the AI. Add this code to the empty Patrol function:

void Patrol()
{
Distance = Vector3.Distance(gameObject.transform.position, Waypoints[curWaypoint].position);
if(Distance > 2.00f)
{
   Destination = Waypoints[curWaypoint].position;
   navAgent.SetDestination(Destination);
}
else
{
   if(ReversePath)
   {
     if(curWaypoint      {
       ReversePath = false;
     }
     else
     {
       curWaypoint--;
       Destination = Waypoints[curWaypoint].position;
     }
   }
   else
   {
     if(curWaypoint >= Waypoints.Length - 1)
     {
       ReversePath = true;
     }
     else
     {
       curWaypoint++;
       Destination = Waypoints[curWaypoint].position;
     }
   }
}
}

What Patrol will now do is check the Distance variable. If it is far from the current waypoint, we set that waypoint as the new destination of our AI. If the current waypoint is close to the AI, we check the ReversePath Boolean variable. When ReversePath is true, we tell the AI to go to the previous waypoint, going through the path in the reverse order. When ReversePath is false, the AI will go on to the next waypoint in the list of waypoints.

With all of this completed, you now have an AI with pathfinding abilities. The AI can also patrol a path set by waypoints and reverse the path when the end has been reached. We have also added abilities for the AI to search for the player as well as flee from the player.

Character animations

Animations are what bring the characters to life visually in the game. From basic animations to super realistic movements, all the animations are important and really represent what scripters do to the player. Before we add animations to our AI, we first need to get a model mesh for it!

Importing the model mesh

For this article, I am using a model mesh that I got from the Unity Asset Store. To use the same model mesh that I am using, go to the Unity Asset Store and search for Skeletons Pack. It is a package of four skeleton model meshes that are fully textured, propped, and animated. The asset itself is free and great to use.

When you import the package into Unity, it will come with all four models as well as their textures, and an example scene named ShowCase. Open that scene and you should see the four skeletons. If you run the scene, you will see all the skeletons playing their idle animations.

Choose the skeleton you want to use for your AI; I chose skeletonDark for mine. Click on the drop-down list of your skeleton in the Hierarchy window, and then on the Bip01 drop-down list. Then, select the magicParticle object. For our AI, we will not need it, so delete it from the Hierarchy window.

Create a new prefab in the Project window and name it Skeleton. Now select the skeleton that you want to use from the Hierarchy window and drag it onto the newly created prefab. This will now be the model that you will use for this article.

In your AI test scene, drag and drop Skeleton Prefab onto the scene. I have placed mine towards the center of the level, near the waypoint in the middle. In the Inspector window, you will be able to see the Animation component full of animations for the model.

Now, we will need to add a few components to our skeleton. Go to the Components menu on the top of the Unity window, select Navigation, and then select NavMesh Agent. Doing this will allow the skeleton to utilize the NavMesh we created earlier. Next, go into the Components menu again and click on Capsule Collider as well as Rigidbody. Your Inspector window should now look like this after adding the components:

 

Your model now has all the necessary components needed to work with our AI script.

Scripting the animations

To script our animations, we will take a simple approach to it. There won’t be a lot of code to deal with, but we will spread it out in various areas of our script where we need to play the animations. In the Idle function, add this line of code:

animation.Play("idle");

This simple line of code will play the idle animation. We use animation to access the model’s animation component, and then use the Play function of that component to play the animation. The Play function can take the name of the animation to call the correct animation to be played; for this one, we call the idle animation.

In the SearchForTarget function, add this line of code to the script:

animation.Play("run");

We access the same function of the animation component and call the run animation to play here. Add the same line of code to the Patrol function as well, since we will want to use that animation for that function too.

In the RangedAttack and MeleeAttack functions, add this code:

animation.Play("attack");

Here, we call the attack animation. If we had a separate animation for ranged attacks, we would use that instead, but since we don’t, we will utilize the same animation for both attack types. With this, we finished coding the animations into our AI. It will now play those animations when they are called during gameplay.

Putting it all together

To wrap up our AI package, we will now finish up the script and add it to the skeleton.

Final coding touches

At the beginning of our AI script, we created some variables that we have yet to properly assign. We will do that in the Start function. We will also add the Update function to run our AI code. Add these functions to the bottom of the class:

void Start()
{
navAgent = GetComponent();

Stats.Add(new KeyValuePair("Health", 100));
Stats.Add(new KeyValuePair("Speed", 10));
Stats.Add(new KeyValuePair("Damage", 25));
Stats.Add(new KeyValuePair("Agility", 25));
Stats.Add(new KeyValuePair("Accuracy", 60));
}

void Update ()
{
RunBehaviors();
}

In the Start function, we first assign the navAgent variable by getting the NavMeshAgent component from the GameObject. Next, we add new KeyValuePair variables to the Stats array. The Stats array is now filled with a few stats that we created.

The Update function calls the RunBehaviors function. This is what will keep the AI running; it will run the correct behavior as long as the AI is active.

Filling out the inspector

To complete the AI package, we will need to add the script to the skeleton, so drag the script onto the skeleton in the Hierarchy window. In the Size property of the waypoints array, type the number 5 and open up the drop-down list. Starting with Element 0, drag each of the waypoints into the empty slots.

For the projectile, create a sphere GameObject and make it a prefab. Now, drag it onto the empty slot next to Projectile. Finally, set the AI Behaviors to Guard. This will make it so that when you start the scene, your AI will be patrolling. The Inspector window of the skeleton should look something like this:

Your AI is now ready for gameplay! To make sure everything works, we will need to do some playtesting.

Playtesting

A great way to playtest the AI is to play the scene in every behavior. Start off with Guard, then run it in Idle, Combat, and Flee. For different outputs, try adjusting some of the variables in the NavMesh Agent component, such as Speed, Angular Speed, and Stopping Distance. Try mixing your waypoints around so the path is different.

Share this post

Leave a Reply