luis-l / UnityAssets

Some Unity assets I worked on
MIT License
38 stars 4 forks source link
agents behavior-trees steering-behaviors unity3d

Unity Assets

  1. Bonsai Behaviour Tree
  2. Autonomous Agents

Bonsai Behaviour Tree

Notice: Bonsai has been moved here for development.

Advanced behavior tree solution with a graphical editor

Bonsai Logo

Bonsai Behaviour Tree Showcase

Videos:

Goals of the project

Features Overview:

Behaviour tree running.

Behaviour tree running

Run-time Editor Features and Limitations

During run-time you can view how the a tree executes and see which nodes are running, the statuses returned (success/failure) or if the nodes were aborted/interrupted.

You can also edit certain properties of a node, like changing the abort type, or setting a new waiting time for the wait task via the Unity Inspector.

Things that cannot be currently edited in run-time are (this may change in the future):

Editor Features

API and Custom Tasks

There are four main categories of nodes which you can extend from to add functionality:

In order to add custom functionality you can override key methods:

    // Called only once when the tree is started.
    public virtual void OnStart() { }

    // The logic that the node executes.
    public abstract Status Run();

    // Called when a traversal begins on the node.
    public virtual void OnEnter() { }

    // Called when a traversal on the node ends.
    public virtual void OnExit() { }

    // The priority value of the node.
    // Default value for all nodes is the negated pre-order index,
    // since lower preorders are executed first (default behaviour).
    public virtual float Priority() { }

    // Called when a child caused an abort.
    protected internal virtual void OnAbort(ConditionalAbort aborter) { }

    // Call when a child finished executing
    protected internal virtual void OnChildExit(int childIndex, Status childStatus) { }

    // Called once after the entire tree is finished being copied.
    // Should be used to setup special BehaviourNode references.
    public virtual void OnCopy() { }

Example of a simple, custom Wait task:

    [NodeEditorProperties("Tasks/", "Timer")]
    public class Wait : Task
    {
        private float _timer = 0f;

        public float waitTime = 1f;

        public override void OnEnter()
        {
            _timer = 0f;
        }

        public override Status Run()
        {
            _timer += Time.deltaTime;

            if (_timer >= waitTime) {
                return Status.Success;
            }

            return Status.Running;
        }
    }

The trickier nodes to extend are composite nodes since they require knowing how to manipulate the "Iterator" in order to traverse nodes. The iterator can be manipulated to dictate how to traverse the tree.

Performance

This is a benchmark running 5000 trees. No GC after startup. The tree in the image is the tree used for benchmark. Tested on a Intel Core i7-4790 @ 4 GHz. (Windows)

Performance Benchmark

I also ran the same benchmark on a Linux laptop. Intel i5-6200U @ 2.30 GHz. The "Time ms" was on average 4 ms.

Limitations

Since I was aiming for a lightweight system, I decided not to provide complete, built-in functionality for serialization (data persistence is not available between running game build sessions). The tree and blackboard structure is saved as an asset, but changing data values will not be persistent between game runs. For example, if you have a blackboard variable ["timer", 0.0f] and during the game run, the value goes up to say 10.0, you would need to save and load that value manually so its persistent between game saves.

I might add a simple system to serialize basic variable types in the blackboard such as int, string, Vector, structs...etc, the difficult, tricky part would be saving persistent object references or very complex objects like dictionaries with objects.

Upcoming Features

Screenshots

The IsKeyDown decorator has a lower priority abort type set, so the sub-trees to the right are highlighted since they can be aborted by the decorator.

Priority Abort

Here the semaphore guards are linked which highlight in orange, you can also see the custom inspector for the guard, making it easy to link other guards together.

Guards

Autonomous Agents

Collection of steering behaviors

Autonomous Agents