Skip to content

7ark/Unity-Basic-Behavior-Tree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

Unity-Basic-Behavior-Tree

Just a basic code-based behavior tree for Unity

Very simple to use. An example:

public class MyClass : Monobehaviour
{
  BehaviorTree tree;
  Vector2[] pathfindingPath = null;
  
  private void Awake()
  {
    //New tree
    tree = new BehaviorTree( 
      //New repeating segement, by default will run forever
      new RepeaterNode(
        //Starts new sequence, the first time it receives a failure from it's child, it will stop
        new Sequence(
          new RunNode(CheckIfPathNull),
          new RunNode(CreateNewPath)
        )
      )
    );
  }
  
  void Update()
  {
    tree.Tick();
  }
  
  private void CheckIfPathNull(Action<NodeResult> result)
  {
    result(pathfindingPath == null ? NodeResult.Succeeded : NodeResult.Failed);
  }
  
  private void CreateNewPath(Action<NodeResult> result)
  {
    pathfindingPath = AStar.CreateNewPath(); //Some function that returns an array of points for A* pathfinding
    result(NodeResult.Succeeded);
  }
}

This will simply keep running (via the RepeaterNode), then in sequence run a set of actions until it receives a failed result. If CheckIfPathNull returns failed, it wont move onto CreateNewPath.

About

Just a basic code-based behavior tree

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages