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.

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.

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.

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.

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.

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.