Behavior Tree Flow

Now that you have an understanding of what a behavior tree is, lets take a look at a specific example. For this example we are going to follow the same setup as the video below:

To get started download the tutorial assets from this link. Open the Behavior Tree scene and notice there is a Behavior Tree added to the Agent GameObject. Duplicate the Agent GameObject and disable the original GameObject. You should then remove the Behavior Tree component so we can start fresh. Rename the duplicated GameObject to “New Agent”. Your hierarchy should now look like:

On the New Agent GameObject add the Behavior Tree component. Lets start with a basic behavior tree that moves the agent to the enemy after a short delay. In order to do this add the Sequence task, followed by the Wait and Seek tasks. Connect the Sequence task to the Entry task and the Wait and Seek tasks to the Sequence task. The Wait task should be to the left of the Seek task:

In behavior tree jargon a Sequence task is similar to an “AND” in the programming world. Behavior trees execute from top to bottom, left to right. Since the Sequence task is connected to Entry the Sequence task will run first. The Sequence task will determine which child it should run, and since it moves from left to right the first task it selects is the Wait task.

After Wait has completed the next task that will run is Seek. Once Seek has run there are no more tasks so the behavior tree will end. Lets see this in action. Before you hit play in the Unity editor first increase the Arrived Distance and then specify the Target that the Seek task should move to.

Now select play in Unity. You’ll see the agent move towards the enemy after the wait time. When the Seek task is within the Arrive Distance the task will complete successfully. And with that, you created your first behavior tree!

With the basics down lets now create a little more complicated behavior tree. We now want to have the agent seek the target after the target is within sight instead of seeking after a delay. In order to accomplish this we are going to add a few new types of tasks. Create a new behavior tree and add the following tasks: Selector, Sequence, Can See Object, Seek, and Idle. Arrange and connect the tasks so you get the following:

Lets complete the setup before by setting the Can See Object Target Object variable to the enemy:

And then set the same variables on the Seek task as before:

With this tree we have a few new concepts. The first concept is the Selector task. If the Sequence task is similar to an “AND” then the Selector is similar to an “OR”. After the tasks execute they will return a status:

  • Running: The task should continue to execute.
  • Failure: The task failed.
  • Success: The task succeeded.

The Selector task will select child tasks from left to right until a child returns a status of success. When this behavior tree starts the Selector task will run the Sequence task, which will then run Can See Object. Since the agent cannot see the target initially the Can See Object task will return a Failure status. When a child of the Sequence task returns failure the Sequence task will also return Failure. The next task that runs is the Selector task again which sees that it has a child that returned Failure so it needs to move onto the next child. Idle is next and the Idle task always returns a status of Running. Go ahead and hit play so you can see this in action:

If Idle always returns a status of Running, how do we stop it from executing? There are a couple of different ways, but the easiest method is to use a concept called Conditional Aborts. After you complete this tutorial you’ll want to go through the Conditional Aborts page/video, but as an overview conditional aborts will reevaluate conditional tasks while other tasks are active. For this tree we are going to set a Lower Priority conditional abort on the Sequence task:

The Lower Priority abort type will reevaluate the child conditional tasks when a branch to the right of the task is active. In this case, since the Idle task has a lower priority than the Sequence task (remember behavior trees execute from left to right, so because Idle is to the right of Sequence it has a lower priority) the Can See Object task will reevaluate. When you hit play you should the Can See Object task has a failure return status icon but that icon also has a loop around it indicating that it is being reevaluated:

Since Can See Object is now being reevaluated when the enemy comes within sight of the agent you’ll see the Can See Object status switch from Failure to Success. The Seek task will now run allowing the agent to reach the enemy.

With this you now have the building blocks to create a much more advanced behavior tree. The links below contain explanation for some more advanced behavior tree setups: