docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Behavior graphs

    A behavior graph is a hierarchical structure. It helps you create artificial intelligence (AI) in video games by enabling agents to dynamically transition between modular sets of tasks. This structure enables the composition of collections of simple tasks into complex, adaptive behaviors, facilitating the creation of sophisticated AI systems.

    Muse Behavior provides a graph-based tool for designing behavior graphs through a graphical user interface. The visual representation of the agent's logic facilitates the understanding of the AI agent's behavior. This enables easier debugging and quicker authoring iterations.

    Each node in the graph contains logic for a single step of the entire behavior. The structure of the graph determines the following items:

    • The run order of the nodes.
    • What happens when nodes succeed or fail.
    • Which nodes will run next based on the previous condition.

    Nodes can be of two types:

    Node types

    • Control flow node: These nodes define how the logic flows through the graph and determine which nodes to run next. The different types of control flow nodes are modifier nodes, sequencing nodes, and join nodes.

    • Action nodes (shown in grey color): These nodes define the actual actions to run that encompass a wide range of tasks. For example:

      • Display a GameObject in the scene.
      • Move the AI agent towards a position.
      • Check for the distance to another target.
      • Store some value for further computation.

      For more information, refer to Action node.

    Note that in the previous diagram, the highlighted box groups two action nodes, but each one is a separate action node.

    For AI to execute an action, the logic flow starts at the top with the start node. It then flows down through the control flow nodes to the action nodes.

    Muse Behavior defines node types as C# classes in the project. Muse Behavior also includes some wizards and utilities to make it easier to create or edit node types.

    In a behavior graph, nodes depict brief, human-readable stories where you can input context-specific data, such as variables, to drive the logic. These variables are relevant and applicable only within a particular scenario represented by a node in the behavior graph. For example, location of the agent in the game world or state of any nearby enemies.

    Nodes

    Nodes are part of the behavior graph that define the flow of the graph. They control the sequence of events determining different paths down the graph, ensuring the agent behaves the way you specify. You can create and connect nodes using the Muse Behavior visual editor. Each node has a particular functionality attached to it.

    The behavior graph nodes are of four types: action node, modifier node, sequencing node, and join node.

    Action node

    Action nodes typically don't have any children and serve as primary means of interaction with the scene and other objects within the behavior graph. They're the most important of node types available.

    An action can succeed or fail. An action can run immediately (in one frame) or require time to finish (for example, walking to a destination).

    Actions are the most common form of node in a behavior graph, with other types of nodes handling the flow of execution between actions. Typically, actions are responsible for moving objects, triggering animations, modifying variable values, or otherwise interacting with the scene.

    For example, you can use the Talk node to display the text spoken by the agent.

    Talk node

    Another example of an action node is Walk. A Walk node makes a character walk to a specific point on the map.

    Modifier node

    Modifier nodes control the execution flow of the behavior graph. They can have only a single child node.

    The modifier nodes perform the following functions:

    • Execution control: They regulate how to run the child branch, such as repeating it or altering its run pattern.

    • Status modification: They modify the status returned by the child node, influencing the behavior of parent control flow nodes.

    You can find most modifiers in the Flow category of the node menu. Some modifiers used in the behavior graph include Repeat, Repeat Until Failure, Repeat Until Success, Repeat While Condition, Succeeder, Inverter, and OnStart.

    For example, you can use a Repeat node to loop indefinite behavior.

    Repeat node

    Another example of a modifier is the Inverter, which inverts the result of the child.

    • If a child fails, it returns success to its parent.
    • If a child succeeds, it returns failure to the parent.

    Sequencing node

    Sequencing nodes define the root of a branch and base rules for how to run the branch. They can have one or more children.

    The most common sequencing node, Sequence, processes its child nodes sequentially, running them from left to right in a step-by-step manner. Each child node runs one at a time, continuing as long as each step succeeds.

    Another example of a sequencing node is Random, which randomly selects one child node to execute from its list of children.

    You can find all sequencing nodes in the Flow category of the node menu. Other sequencing nodes used in the behavior graph include Try In Order and Run In Parallel.

    For example, you can use the Run In Parallel node to run all the branches in parallel.

    Run In Parallel

    Another example of a sequencing node is Sequence, which runs each child in sequence. It returns failure when any of the children fails and returns success when every child returns a successful status.

    Join node

    Join nodes merge the branches so they're run together. Join nodes can have multiple parent branches but only one child branch.

    There are two types of join nodes:

    • Wait For All: Runs its branch after all incoming branches have finished.
    • Wait For Any: Runs its branch after at least one incoming branch has finished.

    Alt text

    Node statuses

    All nodes in the behavior graph exist in one of five possible states, each with a corresponding status label and icon.

    The following table shows the node statuses along with their description.

    Node status Description
    Running The node is currently running.
    Waiting The node is waiting on one or more of its child branches.
    Succeeded The node has succeeded.
    Failed The node has failed.
    Uninitialized The node hasn't yet begun to run.

    Additional resources

    • Behavior graph node types
    • Muse Behavior user interface
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)