Behavior Tree Basics
A behavior tree repeatedly traverses connected tasks to decide what an agent should do. It is useful when the agent needs priorities, fallbacks, sequences, and long-running actions that respond to changing game state.
Understand the four task types
- An Action changes or waits on game state, such as moving, attacking, playing audio, or waiting. It returns Running, Success, or Failure.
- A Conditional checks game state without performing the main action, such as whether a target is visible or health is low.
- A Composite owns several children and decides their order. A Sequence stops on the first failure; a Selector stops on the first success.
- A Decorator owns one child and changes when it runs or how its result is interpreted. Repeaters and inverters are common examples.
Follow task status
TaskStatus.Running keeps the task active for another update. TaskStatus.Success reports that the task finished as intended. TaskStatus.Failure reports that it could not complete under the current conditions. Parent tasks use those results to choose the next child and eventually produce their own result.
For example, a Selector can try an attack Sequence first and an Idle action second. If the attack condition fails, the Sequence fails and the Selector starts Idle. A Conditional Abort can reevaluate the attack condition while Idle is running and switch branches as soon as the target becomes available.
Choose a behavior tree when
- several actions may be valid and priority determines which one wins;
- a failed strategy should fall back to another strategy;
- decisions need to react while a long-running task is active; or
- reusable subtrees keep a large decision system understandable.
For stable modes with explicit transitions, compare Behavior Trees or Finite State Machines. Continue with Flow for a closer look at traversal, or build the Behavior Tree Flow tutorial in Unity.