Behavior tree basics
- Game Development course
- Advanced
- Lesson 40/71
- 4 min
Learn the behavior tree structure and node types.
Behavior Tree Architecture
A Behavior Tree is a decision-making model that organizes complex AI behaviors into a hierarchical tree. Each evaluated node reports one of three states: Success, Failure, or Running.
public enum NodeState {
Success,
Failure,
Running
}The Running status conveys that a time-consuming action, such as walking toward an objective, is actively in progress.
Sequence and Selector Nodes
Two composite control nodes govern tree branching: Sequence (AND logic) and Selector (OR logic).
// Sequence: All child steps must return Success
// [Spot Enemy] -> [Close Distance] -> [Shoot]
// Selector: Halts on the first successful child
// [Flee if Low Health] OR [Patrol Waypoints]Combining these two building blocks yields adaptive and nuanced artificial intelligence behaviors.
Practice Behavior tree node state
Define a Start method. Inside, declare string dugumDurumu = "Success"; and print "Dugum sonucu: " + dugumDurumu to the console.
Starter code
using UnityEngine;
public class BehaviorTreeTester : MonoBehaviour
{
// Start icinde dugumDurumu tanimla ve yazdir
}Sample solution
using UnityEngine;
public class BehaviorTreeTester : MonoBehaviour
{
void Start()
{
string dugumDurumu = "Success";
Debug.Log("Dugum sonucu: " + dugumDurumu);
}
}Expected output
Dugum sonucu: Success
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which three states can a Behavior Tree node return upon evaluation?
- Red, Green, Blue
- Play, Pause, Stop
- Success, Failure, Running Correct answer
- Low, Medium, High
Explanation: Behavior tree nodes return Success on completion, Failure on rejection, and Running when an action is ongoing.
2 Node states enum declaration
public enum NodeState { Success, Failure, ____ }
public enum NodeState { Success, Failure, Running }
Explanation: The 'Running' state signifies that a behavior tree node's action is currently in progress.
3 Behavior tree components
| Term | Meaning |
|---|---|
Sequence |
All child steps must succeed (AND) |
Selector |
Completes on first successful child (OR) |
Success |
Node successfully completing its task |
Running |
Node still executing the current task |