Unreal Engine 4 Tutorial: Artificial Intelligence

In this Unreal Engine 4 tutorial, you will learn how to use behavior trees and AI Perception to create a simple AI character that roams and attacks enemies. By Tommy Tran.

4.8 (29) · 1 Review

Save for later

In video games, Artificial Intelligence (AI) usually refers to how a non-player character makes decisions. This could be as simple as an enemy seeing the player and then attacking. It could also be something more complex such as an AI-controlled player in a real-time strategy.

In Unreal Engine, you can create AI by using behavior trees. A behavior tree is a system used to determine which behavior an AI should perform. For example, you could have a fight and a run behavior. You could create the behavior tree so that the AI will fight if it is above 50% health. If it is below 50%, it will run away.

In this tutorial, you will learn how to:

  • Create an AI entity that can control a Pawn
  • Create and use behavior trees and blackboards
  • Use AI Perception to give the Pawn sight
  • Create behaviors to make the Pawn roam and attack enemies
Note: This tutorial is part of a 10-part tutorial series on Unreal Engine:

Getting Started

Download the starter project and unzip it. Navigate to the project folder and open MuffinWar.uproject.

Press Play to start the game. Left-click within the fenced area to spawn a muffin.

Unreal Engine 4 Particles Tutorial

In this tutorial, you will create an AI that will wander around. When an enemy muffin comes into the AI’s range of vision, the AI will move to the enemy and attack it.

To create an AI character, you need three things:

  1. Body: This is the physical representation of the character. In this case, the muffin is the body.
  2. Soul: The soul is the entity controlling the character. This could be the player or an AI.
  3. Brain: The brain is how the AI makes decisions. You can create this in different ways such as C++ code, Blueprints or behavior trees.

Since you already have the body, all you need is a soul and brain. First, you will create a controller which will be the soul.

What is a Controller?

A controller is a non-physical actor that can possess a Pawn. Possession allows the controller to—you guessed it—control the Pawn. But what does ‘control’ mean in this context?

For a player, this means pressing a button and having the Pawn do something. The controller receives inputs from the player and then it can send the inputs to the Pawn. The controller could also handle the inputs instead and then tell the Pawn to perform an action.

Unreal Engine 4 AI Tutorial

In the case of AI, the Pawn can receive information from the controller or brain (depending on how you program it).

To control the muffins using AI, you need to create a special type of controller known as an AI controller.

Creating an AI Controller

Navigate to Characters\Muffin\AI and create a new Blueprint Class. Select AIController as the parent class and name it AIC_Muffin.

Unreal Engine 4 AI Tutorial

Next, you need to tell the muffin to use your new AI controller. Navigate to Characters\Muffin\Blueprints and open BP_Muffin.

By default, the Details panel should show the default settings for the Blueprint. If it doesn’t, click Class Defaults in the Toolbar.

Unreal Engine 4 AI Tutorial

Go to the Details panel and locate the Pawn section. Set AI Controller Class to AIC_Muffin. This will spawn an instance of the controller when the muffin spawns.

Unreal Engine 4 AI Tutorial

Since you are spawning the muffins, you also need to set Auto Possess AI to Spawned. This will make sure AIC_Muffin automatically possesses BP_Muffin when spawned.

Unreal Engine 4 AI Tutorial

Click Compile and then close BP_Muffin.

Now, you will create the logic that will drive the muffin’s behavior. To do this, you can use behavior trees.

Creating a Behavior Tree

Navigate to Characters\Muffin\AI and select Add New\Artificial Intelligence\Behavior Tree. Name it BT_Muffin and then open it.

The Behavior Tree Editor

The behavior tree editor contains two new panels:

  1. Behavior Tree: This graph is where you will create nodes to make the behavior tree
  2. Details: Nodes you select will display its properties here
  3. Blackboard: This panel will show Blackboard keys (more on this later) and their values. Will only display when the game is running.

Like Blueprints, behavior trees consist of nodes. There are four types of nodes in behavior trees. The first two are tasks and composites.

What are Tasks and Composites?

As its name implies, a task is a node that "does" something. This can be something complex such as performing a combo. It could also be something simple such as waiting.

Unreal Engine 4 AI Tutorial

To execute tasks, you need to use composites. A behavior tree consists of many branches (the behaviors). At the root of each branch is a composite. Different types of composites have different ways of executing their child nodes.

For example, you have the following sequence of actions:

Unreal Engine 4 AI Tutorial

To perform each action in a sequence, you would use a Sequence composite. This is because a Sequence executes its children from left to right. Here’s what it would look like:

Unreal Engine 4 AI Tutorial

Note: Everything beginning from a composite can be called a subtree. Generally, these are your behaviors. In this example, Sequence, Move To Enemy, Rotate Towards Enemy and Attack can be considered the "attack enemy" behavior.

If any of a Sequence’s children fail, the Sequence will stop executing.

For example, if the Pawn is unable to move to the enemy, Move To Enemy will fail. This means Rotate Towards Enemy and Attack will not execute. However, they will execute if the Pawn succeeds in moving to the enemy.

Later on, you will also learn about the Selector composite. For now, you will use a Sequence to make the Pawn move to a random location and then wait.

Moving to a Random Location

Create a Sequence and connect it to the Root.

Unreal Engine 4 AI Tutorial

Next, you need to move the Pawn. Create a MoveTo and connect it to Sequence. This node will move the Pawn to a specified location or actor.

Unreal Engine 4 AI Tutorial

Afterwards, create a Wait and connect it to Sequence. Make sure you place it to the right of MoveTo. Order is important here because the children will execute from left to right.

Unreal Engine 4 AI Tutorial

Note: You can check the order of execution by looking at the numbers at the top-right of each node. Lower numbered nodes have priority over higher numbered nodes.

Congratulations, you have just created your first behavior! It will move the Pawn to a specified location and then wait for five seconds.

To move the Pawn, you need to specify a location. However, MoveTo only accepts values provided through blackboards so you will need to create one.

Tommy Tran


Tommy Tran


Over 300 content creators. Join our team.