Behavior Trees in Unity
How do you create a nice user (developer) friendly Behavior Tree in Unity? The hardest part with BT’s is not making them, it is maintaining them! As they scale they become pretty hard to grasp, and you need good visual tools to debug. But I just got an idea that I want to write down somewhere before I go to sleep and forget all about it.
First some links
Introduction to BT’s, if you don’t know them:
Interesting project with viusal editor (though way to buggy to be used, and also I don’t like the performance warning in the readme):
Here is a rundown on how to create a similar visual editor, using Scriptable Objects:
And lastly, you might have to read up on ScriptableObject as well:
So, ScriptableObject has some problems for our usecase: It does not support polymorphism. So here is the idea on how to solve it.
- A BehaviorTree class, with custom PropertyDrawer
- All the basic nodes are built-in into BehaviorTree, such as Sequence, Parallel, Repeater and so on
- Custom Condition and Action nodes are added as MonoBehaviours to the gameObject (and thereby serialized with all their properties), but added by reference as a nodes in the BehaviorTree
It’s not a pretty solution, but to me it beats most of the other solutions I have seen. Then it’s just a matter of building that custom inspector for the BT.