Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
F

fluid-behavior-tree

> 包管理
Open source

Behavior trees for Unity3D projects. Written with a code driven approach on the builder pattern.

1.2K stars0 likes1 views
WebsiteGitHub

About

Behavior trees for Unity3D projects. Written with a code driven approach on the builder pattern.

Behavior trees for Unity3D projects. Written with a code driven approach to maximize maintainability on large projects with the builder pattern. Inspired by Fluent Behavior Tree.

Features

  • Extendable, write your own custom re-usable nodes
  • Pre-built library of tasks to kickstart your AI
  • Tree visualizer to debug your trees at runtime
  • Heavily tested with TDD and unit tests
  • Tracks the last position of your behavior tree and restores it the next frame
  • Built for Unity (no integration overhead)

Support

Join the Discord Community if you have questions or need help.

See upcoming features and development progress on the Trello Board.

Getting Started

When creating trees you'll need to store them in a variable to properly cache all the necessary data.

…

What a Returned TaskStatus Does

Depending on what you return for a task status different things will happen.

  • Success: Node has finished, next tree.Tick() will restart the tree if no other nodes to run
  • Failure: Same as success, except informs that the node failed
  • Continue: Rerun this node the next time tree.Tick() is called. A pointer reference is tracked by the tree and can only be cleared if tree.Reset() is called.

Tree Visualizer

As long as your tree storage variable is set to public or has a SerializeField attribute. You'll be able to print a visualization of your tree while the game is running in the editor. Note that you cannot view trees while the game is not running. As the tree has to be built in order to be visualized.

Extending Trees

You can safely add new code to your behavior trees with several lines. Allowing you to customize BTs while supporting future version upgrades.

…

Installing

Fluid Behavior Tree is used through Unity's Package Manager. In order to use it you'll need to add the following lines to your Packages/manifest.json file. After that you'll be able to visually control what specific version of Fluid Behavior Tree you're using from the package manager window in Unity. This has to be done so your Unity editor can connect to NPM's package registry.

json
{
  "scopedRegistries": [
    {
      "name": "NPM",
      "url": "https://registry.npmjs.org",
      "scopes": [
        "com.fluid"
      ]
    }
  ],
  "dependencies": {
    "com.fluid.behavior-tree": "2.2.0"
  }
}

Archives of specific versions and release notes are available on the releases page.

Example Scene

You might want to look at the capture the flag example project for a working example of how Fluid Behavior Tree can be used in your project. It demonstrates real time usage with units who attempt to capture the flag while grabbing power ups to try and gain the upper hand.

Table of Contents

  • Library
    • Actions
      • Generic
      • Wait
      • Wait Time
    • Conditions
      • Generic
      • RandomChance
    • Composites
      • Sequence
      • Selector
      • SelectorRandom
      • Parallel
    • Decorators
      • Generic
      • Inverter
      • ReturnSuccess
      • ReturnFailure
      • RepeatForever
      • RepeatUntilFailure
      • RepeatUntilSuccess
  • Creating Reusable Behavior Trees
  • Creating Custom Reusable Nodes
    • Your First Custom Node and Tree
    • Custom Actions
    • Custom Conditions
    • Custom Composites
    • Custom Decorators
  • Formatting Issues
  • Nightly Builds
  • Development Environment
    • Submitting your own actions, conditions, ect
    • Pull Requests / Contributing
  • Contributor Credits

Example Scene

You might want to look at the capture the flag example project for a working example of how Fluid Behavior Tree can be used in your project. It demonstrates real time usage with units who attempt to capture the flag while grabbing power ups to try and gain the upper hand.

Library

Fluid Behavior Tree comes with a robust library of pre-made actions, conditions, composites, and other nodes to help speed up your development process.

Actions

Action Generic

You can create a generic action on the fly. If you find yourself re-using the same actions you might want to look into the section on writing your own custom actions.

csharp
.Sequence()
    .Do("Custom Action", () => {
        return TaskStatus.Success;
    })
.End()

Wait

Skip a number of ticks on the behavior tree.

csharp
.Sequence()
    // Wait for 1 tick on the tree before continuing
    .Wait(1)
    .Do(MyAction)
.End()

Wait Time

Waits until the passed number of seconds have expired in deltaTime.

csharp
.Sequence()
    .WaitTime(2.5f)
    .Do(MyAction)
.End()

Conditions

Condition Generic

You can create a generic condition on the fly. If you find yourself re-using the same actions you might want to look into the section on writing your own custom conditions.

csharp
.Sequence()
    .Condition("Custom Condtion", () => {
        return true;
    })
    .Do(MyAction)
.End()

RandomChance

Randomly evaluate a node as true or false based upon the passed chance.

csharp
.Sequence()
    // 50% chance this will return success
    .RandomChance(1, 2)
    .Do(MyAction)
.End()

Composites

Sequence

Runs each child node in order and expects a Success status to tick the next node. If Failure is returned, the sequence will stop executing child nodes and return Failure to the parent.

NOTE It's important that every composite is followed by a .End() statement. This makes sure that your nodes are properly nested when the tree is built.

csharp
.Sequence()
    .Do(() => { return TaskStatus.Success; })
    .Do(() => { return TaskStatus.Success; })
    
    // All tasks after this will not run and the sequence will exit
    .Do(() => { return TaskStatus.Failure; })

    .Do(() => { return TaskStatus.Success; })
.End()

Selector

Runs each child node until Success is returned.

csharp
.Selector()
    // Runs but fails
    .Do(() => { return TaskStatus.Failure; })

    // Will stop here since the node returns success
    .Do(() => { return TaskStatus.Success; })
    
    // Does not run
    .Do(() => { return TaskStatus.Success; })
.End()

SelectorRandom

Randomly selects a child node with a shuffle algorithm. Looks until Success is returned or every node fails. Shuffles every time the tree initially start running it.

csharp
.SelectorRandom()
    .Do(() => { return TaskStatus.Failure; })
    .Do(() => { return TaskStatus.Success; })
    .Do(() => { return TaskStatus.Failure; })
.End()

Parallel

Runs all child nodes at the same time until they all return Success. Exits and stops all running nodes if ANY of them return Failure.

csharp
.Parallel()
    // Both of these tasks will run every frame
    .Do(() => { return TaskStatus.Continue; })
    .Do(() => { return TaskStatus.Continue; })
.End()

Decorators

Decorators are parent elements that wrap any node to change the return value (or execute special logic). They are extremely powerful and a great compliment to actions, conditions, and composites.

Decorator Generic

You can wrap any node with your own custom decorator code. This allows you to customize re-usable functionality.

NOTE: You must manually call Update() on the child node or it will not fire. Also every decorator must be followed by a .End() statement. Otherwise the tree will not build correctly.

csharp
.Sequence()
    .Decorator("Return Success", child => {
        child.Update();
        return TaskStatus.Success;
    })
        .Do(() => { return TaskStatus.Failure; })
    .End()
    .Do(() => { return TaskStatus.Success; })
.End()

Inverter

Reverse the returned status of the child node if it's TaskStatus.Success or TaskStatus.Failure. Does not change TaskStatus.Continue.

csharp
.Sequence()
    .Inverter()
        .Do(() => { return TaskStatus.Success; })
    .End()
.End()

ReturnSuccess

Return TaskStatus.Success if the child returns TaskStatus.Failure. Does not change TaskStatus.Continue.

csharp
.Sequence()
    .ReturnSuccess()
        .Do(() => { return TaskStatus.Failure; })
    .End()
.End()

ReturnFailure

Return TaskStatus.Failure if the child returns TaskStatus.Success. Does not change TaskStatus.Continue.

csharp
.Sequence()
    .ReturnFailure()
        .Do(() => { return TaskStatus.Success; })
    .End()
.End()

RepeatForever

Return TaskStatus.Continue regardless of what status the child returns. This decorator (and all descendent tasks) can be interrupted by calling BehaviorTree.Reset().

csharp
.Sequence()
    .RepeatForever()
        .Do(() => { return TaskStatus.Success; })
    .End()
.End()

RepeatUntilFailure

Return TaskStatus.Failure if the child returns TaskStatus.Failure, otherwise it returns TaskStatus.Continue.

csharp
.Sequence()
    .RepeatUntilFailure()
        .Do(() => { return TaskStatus.Success; })
    .End()
.End()

RepeatUntilSuccess

Return TaskStatus.Success if the child returns TaskStatus.Success, otherwise it returns TaskStatus.Continue.

csharp
.Sequence()
    .RepeatUntilSuccess()
        .Do(() => { return TaskStatus.Success; })
    .End()
.End()

Creating Reusable Behavior Trees

Trees can be combined with just a few line of code. This allows you to create injectable behavior trees that bundles different nodes for complex functionality such as searching or attacking.

Be warned that spliced trees require a newly built tree for injection, as nodes are only deep copied on .Build().

…

Creating Custom Reusable Nodes

What makes Fluid Behavior Tree so powerful is the ability to write your own nodes and add them to the builder without editing any source. You can even create Unity packages that add new builder functionality. For example we can write a new tree builder method like this that sets the target of your AI system with just a few lines of code.

csharp
var tree = new BehaviorTreeBuilder(gameObject)
    .Sequence()
        .AgentDestination("Find Enemy", target)
        .Do(() => {
            // Activate chase enemy code
            return TaskStatus.Success; 
        })
    .End()
    .Build();

Your First Custom Node and Extension

It should take about 3 minutes to create your first custom action and implement it. First create a new action.

csharp
using CleverCrow.Fluid.BTs.Tasks;
using CleverCrow.Fluid.BTs.Tasks.Actions;
using UnityEngine;
using UnityEngine.AI;

public class AgentDestination : ActionBase {
    private NavMeshAgent _agent;
    public Transform target;

    protected override void OnInit () {
        _agent = Owner.GetComponent();
    }

    protected override TaskStatus OnUpdate () {
        _agent.SetDestination(target.position);
        return TaskStatus.Success;
    }
}

Next we need to extend the BehaviorTreeBuilder script with our new AgentDestination action. For more information on C# class extensions see the [official docs](ht

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#aibehavior-treebuilder-patternunity-package-manager

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category包管理
PricingOpen source

> Related tools

N
npm
Node.js 默认包管理器
P
pnpm
高效节省磁盘的 Node 包管理器
I
is-gif
Check if a Buffer/Uint8Array is a GIF image