Behavior Trees and their implementation in Unity

Machine Learning Artificial Intelligence Natural Language Processing Artificial Intelligence Algorithm Artificial Life and Agent Reasoning Technology Knowledge Information ICT Technology Digital Transformation Automata/State Transitions/Automatic Programming Navigation of this blog
Behavior Trees

Behavior Tree is a framework for constructing complex AI behaviors that are also used in game AI. Originally developed for the purpose of robotics, it is now used as an improved version of the hierarchical state machine for designing AI for non-player characters (NPCs) in games such as FPS. The advantage is that it is easy to design and implement, reusable and portable, and can support large and complex logic.

A state machine is defined as “a mathematically abstracted behavioral model consisting of a finite number of states, transitions, and actions. In contrast, the Behavior Tree has the problem of low reusability.

On the other hand, Behavior Tree forms a tree-like structure by mutually nesting states, and enhances modularity by restricting transitions to only these nested states.

The specific structure of Behavior Tree is as follows: Behavior is described by a tree structure or DAG, and a group of tasks is a subtree. There are three types of nodes that make up a tree: root nodes, control flow nodes, and leaf nodes (task nodes). The evaluation of these nodes is done by depth-first search, and information is returned from the child nodes to the parent node as a result of the search. The search results are Success, Failure, and Running, and all nodes can be set to active or inactive status to indicate whether they can be evaluated.

wiki「Behavior tree」より

Control Flow Nodes are used to control subtasks, and there are two types: selector nodes and sequence nodes. Selector nodes are used to find and execute the first child that does not fail. If any of the Selector’s children return Success or Running, the Selector immediately returns Success or Running to the parent node. If all the child nodes of the Selector return failure, the Selector also returns failure to the parent node.

In contrast, a sequence node is a node that executes its children in order, and is evaluated in a depth-first search from left to right (high priority to low priority). If any of the child nodes of the Sequence returns Failure or Running, the Selector immediately returns Failure or Running to the parent node; if all the child nodes of the Selector return Success, the Selector also returns Success to the parent node.

Here, selector and sequence nodes each have node variations that operate in a “random” manner, and there are also “decorators” for executing sequences such as if, while, loop, etc., and “parallel” nodes for parallel operation.

The final action is described in the most terminal leaf node. The description of this leaf node can be divided into two categories: actions and conditions. An action is the execution of a function or method. In the case of a game, this would be the execution of a function or method, such as moving a character or decreasing its health. Conditions introduce the state of objects in the game world. For example, the character’s position, the amount of health, etc.

In a state machine, state transitions are shown, so it takes a bit more to describe actions from there, but in a behavior tree, they are described straightforwardly.

behavior tree with unity using unity

In Unity, there are different ways to implement Behavior Trees (Behavior Trees). Behavior trees are graph-based algorithms for controlling the behavior of game characters and AI agents. Unity has several dedicated frameworks and assets for creating behavior trees, but here we describe the basic methods.

The general approach to implementing behavior trees in Unity would be to use scripting to create a custom behavior tree system. Here are the basic steps of this approach

  1. Create a node class: A behavior tree consists of small blocks of behaviors called nodes. Node classes include methods for performing the actual behavior, references to child nodes, etc.
  2. Creating node types: An action tree is composed of different types of nodes, such as sequences, selectors, decorators, and so on. Each node has a different behavior. This means, for example, that a sequence node executes child nodes in sequence and returns success only if all succeed; a selector node executes child nodes in sequence and returns success if the first succeeds; and a decorator node modifies the behavior of child nodes. Classes of these nodes can be created and can have additional parameters if needed.
  3. Behavior tree construction: To construct a behavior tree, a hierarchical structure is created by combining node classes. Parent nodes have references to child nodes, and conditions and control flow can be set as needed.
  4. Executing actions: To execute the created action tree, the root node must be invoked. The root node executes the child nodes in turn and returns the final result. The execution of the action is usually incorporated into every frame update or event trigger.

The above approach can be used to implement a custom behavior tree system. However, there are also a number of extensions and plug-ins for creating behavior trees in the Unity asset store that can be leveraged as needed. This allows you to create and manage behavior trees in a more efficient and visual way.

Behavior trees are a powerful way to control the complex behavior of AI agents, and using Unity to implement behavior trees allows for flexible control over the behavior of game characters and AI characters.

Implementing Behavior Trees Using Unity’s Asset Store

Unity’s asset store has a variety of assets for implementing behavior trees. Using these assets, you can easily and efficiently create behavior trees by utilizing the visual editor and pre-created nodes and blocks. Some popular assets are discussed below.

  • Behavior Designer: Behavior Designer is an asset that provides a powerful behavior tree system. This asset includes a visual editor that allows users to create behavior trees by dragging and dropping nodes and connecting them. Many custom nodes are also provided, including conditions and decorators.
  • NodeCanvas: NodeCanvas will be a behavior tree framework with a visual editor and custom nodes. The asset can incorporate not only behavior trees, but also other AI control methods such as state machines and dialog trees.
  • Playmaker: Playmaker is a graph-based playmaker system that can be used as a powerful tool for implementing behavior trees. This asset provides a visual state machine editor to facilitate the creation and control of behavior trees.

These assets can be downloaded from Unity’s asset store and imported into a project, which includes a visual editor, custom nodes, sample scripts, and more. By utilizing these assets, you can create behavior trees and control the behavior of AI characters in a visual and efficient manner. However, it is important to check the license terms when using assets from the asset store. Also, understanding the usage and documentation of the assets and taking advantage of any support that may be available from the asset provider should also be considered.

コメント

タイトルとURLをコピーしました