Behavior tree
Overview
Behavior trees (BTs) are a potent tool for designing AI logic visually without coding. Use the behavior tree editor (BTE) to add, edit, and connect task nodes, forming tree structures that define behaviors for objects or non-player characters (NPCs). Besides streamlining AI logic creation, BTs also make debugging easier as you can monitor node status in BTs in Play Mode.
Adding a behavior tree
There are three methods to add a BT:
Method 1 and 2 require you to configure a BT from scratch on your own, but Method 3 adds a prefab with prepared BT logic.
-
Method 1: In Scene Explorer, click the + icon and select Behavior Tree.
-
Method 2: Add an object to the scene first. Then, in its Properties window, click Add Components, search for Behavior Tree Component, and click to add it.
-
Method 3: In Packages, search for the Behavior Tree NPC prefab and click to add it. This prefab has a prepared behavior tree that dictates the NPC to seek the player once the player enters its view.
After adding:
To edit the Behavior Tree component, right click the prefab and unpack it.
Behavior Tree Editor (BTE)
Accessing the BTE
Once a behavior tree is added, click Edit Behavior Tree in its BT Component to open the behavior tree editor (BTE).
Understanding the BTE interface
-
Section 1: Graph area
This area displays the entire behavior tree. Each grey rectangle is a node (or a task) which undertakes a specific operation. The nodes are connected by the purple lines which indicate their relationships. Use the scroll bars at the bottom and right to move the canvas.
-
Section 2: Sidebar
The sidebar displays settings for the tree and its nodes, consisting of three tabs: Behavior, Variables, and Properties.
- The Behavior tab defines the basic information of the BT, including BT name, description, variables and basic options.
- The Variables tab defines variables associated with the BT, supporting 8 data types: string, number, boolean, color, object, object list, vector 2, and vector 3.
- The Properties tab displays the settings of a specific task node, and different tasks require different properties.
Task types in behavior tree
Behavior trees are a collection of tasks. Tasks are categorized into four types: action, conditional, composite, and decorator.
- Action: Alters the state of the game in some way, the most basic task. In the tree above, GetPlayer and Seek are two action tasks. GetPlayer gets the position of the player, and Seek task drives the NPC to seek the player.
- Conditional: Tests some property of the game. In the tree above, CanSeeObject is a conditional task to test whether the player is within the view of the NPC.
- Composite: A parent task that holds a list of child tasks. Composite task usually includes sequence task, parallel task, and selector task.
- A sequence task runs each of its child tasks once in order and returns success when all the child tasks succeed.
- A parallel task runs all of its child tasks simultaneously and returns success when all the child tasks succeed.
- A selector task runs one of its child tasks and returns success as long as one child task succeeds. In the tree above, Sequence is a composite task which will sequentially get the player position, test if the player is within the view, and seek the player.
- Decorator: A parent task that can only have one child. Its function is to modify the behavior of the child task in some way. There is no decorator in the tree above, but in some cases you may want to use one. For example, you can add a decorator to interrupt an agent from performing a task such as collecting resources when an enemy is nearby, which is called an interrupt task.
Task execution order
Behavior tree will execute the tasks in a depth first order. In the example tree below, the tasks execute in the following order: Sequence, Selector A, Action A, Action B, Selector B, Action C, Action D.
Monitoring behavior tree status
-
In Play Mode, press Alt and click at the bottom. Select a behavior tree to see the task status.
-
You cannot edit the BT in Play Mode, but you can view the node status. A node flashing with green is running; a green icon at the top right of a node means success; a red icon at the top right of a node means failure.