Introduction
In a previous article, I’ve introduced the basic concept of Behavior Trees. In this one, we look into how we could control an AI character with BTs in Unreal Engine 5.
If you already have a project with an NPC character, you can use that, but to make this easy to understand for everyone, I will start from an empty Third Person template project.
The goal
In this example, we will make the NPC patrol around your map by selecting random positions. If it sees the player, it starts chasing it. After getting out of its sight, it will forget the player in a few seconds and return to patrol.
Preparation
First, we duplicate the BP_ThirdPersonCharacter of the Third Person template to use it as an AI controlled character. If you already have your own character, you can skip this step.
Then, we need to create 4 things:
- AI Controller Blueprint, which will possess the NPC character and run the BT.
- Behavior Tree, which tells the character what to do and when
- Blackboard that stores information for the BT
- Navigation Mesh (or NavMesh for short), which makes it possible for the AI to find paths between 2 positions.
Let’s start with the NavMesh. Simply add a Nav Mesh Bounds Volume to your level and scale it to cover all ground that you want to make walkable. If you press the P key in the editor, you can see the result: the reachable area will be covered with a green color.

Next, we create the Blackboard and the Behavior Tree asset. Let’s call them BT_Example and BB_Example.

Open your new Behavior Tree blueprint, the BT_Example and set the Blackboard asset. As we have only one Blackboard in the project, it will find it automatically, but it is important to remember this step: check it and set it to the correct BB asset.

When we have a BT and a BB assigned to each other, we can switch between them easily at the top right corner.

Finally, we create a new Blueprint selecting AIController as a parent class.

For now, the only thing we need to do in this AI Controller BP is to run the Behavior Tree when the Pawn gets possessed. Implement the Event On Possess event, and connect it to a Run Behavior Tree node. Set the BT asset to BT_Example.

As a last step, in the Blueprint of the character, we have to set the AI Controller Class to our newly created AI Controller BP.

Now, we have a Behavior Tree controlled character that does nothing. Time to build a basic behavior for it! Let’s return to the BT_Example Behavior Tree.
As a start, we lay out the structure of what we want to do. The plan is to chase the player when the AI sees it; otherwise, just patrol at random locations. This needs a Selector that will make only one of these branches execute (the first successful) and a Sequence for both the Chase and the Patrol. It is a good practice to give the nodes a name, so it will be easier to read what the BT does.

We want to execute the Chase branch only when we have a target, the player, so we will need a decorator for that, but first we need to create our variables on the Blackboard for it. Switch to the Blackboard and create the following variables:
- TargetActor (Key Type: Object, Base Class: Actor) for the target.
- TargetLocation (Key Type: Vector) for the patrol points.
Save and return to the Behavior Tree.
Decorators in UE5 are not separate nodes but are attached to other nodes. You can right-click on the Chase Sequence node and select Decorator/Blackboard. This type of Decorator is used to check Blackboard variables.

Set the Key Query to Is Set and the Blackboard Key to TargetActor. This means the node with this Decorator will only execute if the TargetActor is set; otherwise, instantly fails. We also set Observer aborts to Self, so the branch will be interrupted if the TargetActor is changed.
Seeing and targeting the player needs additional work, so for now, we leave the Chase branch and focus on the Patrol. This only needs two things: finding a random position on the NavMesh and telling the character to move there. The TargetLocation is communicated through the Blackboard.
Let’s create our first BT task by clicking on the New Task button on the top bar. Select BTTask_BlueprintBase as a parent. We will call this BTTask_FindRandomLocation.
Execution of a BT Task starts with the Event Receive Execute AI node and finishes with Finish Execute. Do not forget the latter, as the BT will not continue the execution if that node is missing. Finish Execute also tells the result with the Success (bool) output pin. For now, we just check it to make it always report true (Success).
Between the two nodes, we want to:
- Get a random position on the NavMesh using the Get Random Reachable Point in Radius node. The Origin is the position of the actor. Promote the Radius pin to variable and make it instance editable, so our task will have a parameter that can be set.
- Set the position on the Blackboard to TargetLocation. This can be done via the Set Value as Vector node. This needs a Blackboard reference, which you can get from the Owner Controller. Create a variable for the Key Name, and set its value for TargetLocation.
Our task BP should look like this:

Returning to the Behavior Tree, our new task now became available to use. Place it under the Patrol sequence and set the Radius to something that makes sense (for example, 1000 to search positions in a 10 meter radius). Next to it, add a Move To node, and set the Blackboard Key to TargetLocation.

If you start the game now, the AI character will run around aimlessly, choosing random positions. If you keep the BT blueprint open, you can actually see what branches are executing, which makes debugging easier when something seems incorrect.
Time to Chase the player. Our AI Character currently can’t perceive anything; we need to make it able to see things. Open up the blueprint of the character and add an AIPerception component. This component can make AI sensitive to different types of stimuli, seeing and hearing things. Select the component, search for Sense Config and add a new element. Set it to AI Sight config. Now the AI has vision! You can fine-tune a bunch of parameters here, like how far it should see and in what radius, but for the example, it is perfectly fine to leave most of it on default, except one parameter. Inside Sense, Detection by Affiliation, we should check Detect Neutrals as the AI currently has no idea who is friend or foe, everything is neutral.
When something is perceived, the Target Perception Updated event is generated, so we need to implement that node. What we want to do here is, whatever the AI saw, set it in the Blackboard as a TargetActor. Also, to make it forget after some time, we can create a timer and clear the Blackboard variable when it expires. Something like this:

Returning to the Behaviour tree, now we can complete the Chase branch by adding a Move To node and aiming for the TargetActor BB key. If you start the game now, the AI will follow the player character, and if it gets out of line of sight, forgets it and goes back to patrol.
We can add another Decorator to the Patrol branch that checks if TargetActor is not set. This can interrupt the patrol immediately when the player is perceived.

And that is it. There are many things we can improve, but for this simple example, we achieved our goal. You can try and make Tasks for setting the movement speed of the NPC to walking speed when patrolling and running speed when Chasing the player, for example.
Conclusion
I think this example project introduces all the necessary steps to set up a Behavior Tree-based AI for a character. With BT tasks, you can create all the functionality that is required for your project and build the BT for your needs.
As a next step, later I will write about how to use UE5’s GAS (Gameplay Ability System) from a Behavior Tree to make your NPC use Abilities and make decisions based on Gameplay Attributes.