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
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

Creating a Blackboard

A blackboard is an asset whose sole function is to hold variables (known as keys). You can think of it as the AI’s memory.

While you are not required to use them, blackboards offer a convenient way to read and store data. It is convenient because many of the nodes in behavior trees only accept blackboard keys.

To create one, go back to the Content Browser and select Add New\Artificial Intelligence\Blackboard. Name it BB_Muffin and then open it.

The Blackboard Editor

The blackboard editor consists of two panels:

Unreal Engine 4 AI Tutorial

  1. Blackboard: This panel will display a list of your keys
  2. Blackboard Details: This panel will display the properties of the selected key

Now, you need to create a key that will hold the target location.

Creating the Target Location Key

Since you are storing a location in 3D space, you need to store it as a vector. Click New Key and select Vector. Name it TargetLocation.

Unreal Engine 4 AI Tutorial

Next, you need a way to generate a random location and store it in the blackboard. To do this, you can use the third type of behavior tree node: service.

What is a Service?

Services are like tasks in that you use them to do something. However, instead of making the Pawn perform an action, you use services to perform checks or update the blackboard.

Services are not individual nodes. Instead, they attach to tasks or composites. This results in a more organized behavior tree because you have less nodes to deal with. Here’s how it would look using a task:

Unreal Engine 4 AI Tutorial

Here’s how it would look using a service:

Unreal Engine 4 AI Tutorial

Now, it’s time to create a service that will generate a random location.

Creating a Service

Go back to BT_Muffin and click New Service.

Unreal Engine 4 AI Tutorial

This will create a new service and open it automatically. Name it BTService_SetRandomLocation. You’ll need to go back to the Content Browser to rename it.

The service only needs to execute when the Pawn wants to move. To do this, you need to attach it to MoveTo.

Open BT_Muffin and then right-click on MoveTo. Select Add Service\BTService Set Random Location.

Unreal Engine 4 AI Tutorial

Now, BTService_SetRandomLocation will activate when MoveTo activates.

Next, you need to generate a random target location.

Generating a Random Location

Open BTService_SetRandomLocation.

To know when the service activates, create an Event Receive Activation AI node. This will execute when the service’s parent (the node it’s attached to) activates.

Unreal Engine 4 AI Tutorial

Note: There is also another event called Event Receive Activation that does the same thing. The difference between the two events is that Event Receive Activation AI also provides the Controlled Pawn.

To generate a random location, add the highlighted nodes. Make sure to set Radius to 500.

Unreal Engine 4 AI Tutorial

This will give you a random navigable location within 500 units of the Pawn.

Unreal Engine 4 AI Tutorial

If you would like to create your own NavMesh, create a Nav Mesh Bounds Volume. Scale it so that it encapsulates the area you want to be navigable.

Note: GetRandomPointInNavigableRadius uses navigation data (called NavMesh) to determine if a point is navigable. In this tutorial, I have already created the NavMesh for you. You can visualize it by going to the Viewport and selecting Show\Navigation.

Next, you need to store the location into the blackboard. There are two ways of specifying which key to use:

  1. You can specify the key by using its name in a Make Literal Name node
  2. You can expose a variable to the behavior tree. This will allow you to select a key from a drop-down list.

You will use the second method. Create a variable of type Blackboard Key Selector. Name it BlackboardKey and enable Instance Editable. This will allow the variable to appear when you select the service in the behavior tree.

Unreal Engine 4 AI Tutorial

Afterwards, create the highlighted nodes:

Unreal Engine 4 AI Tutorial

Summary:

  1. Event Receive Activation AI executes when it’s parent (in this case, MoveTo) activates
  2. GetRandomPointInNavigableRadius returns a random navigable location within 500 units of the controlled muffin
  3. Set Blackboard Value as Vector sets the value of a blackboard key (provided by BlackboardKey) to the random location

Click Compile and then close BTService_SetRandomLocation.

Next, you need to tell the behavior tree to use your blackboard.

Selecting a Blackboard

Open BT_Muffin and make sure you don’t have anything selected. Go to the Details panel. Under Behavior Tree, set Blackboard Asset to BB_Muffin.

Unreal Engine 4 AI Tutorial

Afterwards, MoveTo and BTService_SetRandomLocation will automatically use the first blackboard key. In this case, it is TargetLocation.

Unreal Engine 4 AI Tutorial

Finally, you need to tell the AI controller to run the behavior tree.

Running the Behavior Tree

Open AIC_Muffin and connect a Run Behavior Tree to Event BeginPlay. Set BTAsset to BT_Muffin.

Unreal Engine 4 AI Tutorial

This will run BT_Muffin when AIC_Controller spawns.

Click Compile and then go back to the main editor. Press Play, spawn some muffins and watch them roam around.

Unreal Engine 4 AI Tutorial

That was a lot of set up but you got there! Next, you will set up the AI controller so that it can detect enemies within its range of vision. To do this, you can use AI Perception.

Setting Up AI Perception

AI Perception is a component you can add to actors. Using it, you can give senses (such as sight and hearing) to your AI.

Open AIC_Muffin and then add an AIPerception component.

Unreal Engine 4 AI Tutorial

Next, you need to add a sense. Since you want to detect when another muffin moves into view, you need to add a sight sense.

Select AIPerception and then go to the Details panel. Under AI Perception, add a new element to Senses Config.

Unreal Engine 4 AI Tutorial

Set element 0 to AI Sight config and then expand it.

Unreal Engine 4 AI Tutorial

There are three main settings for sight:

  1. Sight Radius: The maximum distance the muffin can see. Leave this at 3000.
  2. Lose Sight Radius: If the muffin has seen an enemy, this is how far the enemy must move away before the muffin loses sight of it. Leave this at 3500.
  3. Peripheral Vision Half Angle Degrees: How wide the muffin’s vision is. Set this to 45. This will give the muffin a 90 degree range of vision.

Unreal Engine 4 AI Tutorial

By default, AI Perception only detects enemies (actors assigned to a different team). However, actors do not have a team by default. When an actor doesn’t have a team, AI Perception considers it neutral.

As of writing, there isn’t a method to assign teams using Blueprints. Instead, you can just tell AI Perpcetion to detect neutral actors. To do this, expand Detection by Affiliation and enable Detect Neutrals.

Unreal Engine 4 AI Tutorial

Click Compile and then go back to the main editor. Press Play and spawn some muffins. Press the key to display the AI debug screen. Press 4 on the numpad to visualize AI Perception. When a muffin moves into view, a green sphere will appear.

Unreal Engine 4 AI Tutorial

Next, you will move the muffin towards an enemy. To do this, the behavior tree needs to know about the enemy. You can do this by storing a reference to the enemy in the blackboard.

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.