In my latest project, I tried to implement a behavior tree for my AI.
I first discovered the behavior tree system in the Unreal Engine 4, which has a full graphical interface to easily create behaviour trees. And as it was quite a neat feature, I wanted to try implementing my own in Unity3D.
I first created a Node class, from which the Selector, Sequence and Leaf class would derive from. Each of those class contain an array of following Node, allowing to navigate in the tree.
A class Decorator would act as a « if », and when arriving on a Node, it would first GetComponent<Decorator>(), meaning : look for every script of class type Decorator, and test them. For example, I created a Decorator_HasRangedWeapon, or Decorator_IsHurt, etc…
For each new need, I just have to create a children of Decorator and override the Try() function which, well, try if the condition is met.
In the end, it worked quite smoothly, even though for now I don’t have yet very complex trees.
Below a Selector type Node, with two decorator. If the character controlled by the AI has the required PsyStat and has a weapon, the Node will be considered a success and pass to the next Node (Melee)