Unity Behavior Trees

PROJECT NAME:

Unity Behavior Trees

SHORT DESCRIPTION:

Node-based Behavior tree system editor in Unity.

ENGINE:

Unity

DATE:

2016

PROJECT DETAILS

While working on the Mech Simulator project, it was my responsibility to create the AI for the enemies. After working with Unreal Engine 4’s behavior tree system, I was wondering why such an system was not built into Unity. I decided I wanted to learn how behavior trees work in depth by implementing behavior trees into Unity. I also wanted a visual editor for these trees similar to unreal engine’s implementation, making the system more accessible for non-programmers. By doing this I also gained experience in writing Unity editor plugins.

CUSTOM EDITOR & ASSET TYPE

The behaviour tree system heavily relies on C# reflection of metadata and is fully integrated into Unity as custom asset types, with their own editor window associated to these assets; This allows for people with minimum technical knowledge to create and modify behavior tree systems. Behavior trees are created as templates and can be referenced by behavior tree components in entities.

EASE OF USE

By using C#’s reflection system, we can apply meta tags to properties and classes. The plugin heavily relies on these meta tags to allow the editor to automatically register new node types and also allows the editor to display the variables we can modify.

PERFORMANCE

The editor heavily relies on reflection, however in real-time applications this can come with a big performance cost, that’s why the in-game runnable code does not use the reflection system and is aimed to be as light as possible for execution. The developer can also specify fixed update frequencies to make the AI behavior even lighter by only allowing it to update every (n) ms or ticks.

Each type of behavior tree only exists once in memory, but each actor has their own instance of a “blackboard”. The blackboard allows the behaviour tree to store per-entity data. By doing this we can save memory as we can guarantee only a single instance of a type of behaviour tree is present in memory and only the minimal amount of memory is used.