This system came about because I wanted to develop a tool for the designers that I was working on The Resonant Blade with to be able edit the behaviour tree being utilised for the AI. There was no UI in the first iterations of the AI system for the game and I was hard coding the tree itself. So I went through and utilised Unity's new UI Toolkit to create a window that allows the creation of visually scripted nodes of a behaviour tree. The purpose of the AI in The Resonant Blade was to utilise it alongside Root Motion Animation so that I could let the animator's work shine.
I started the first iteration of this code in a very small blunt format that was creating the nodes of the behaviour tree directly in the class' Start function. Manually laying each branch out with new node classes. The more I had to dive in and edit this layout the more it became obvious I needed a tool/refactor of the entire system. There were other bugs arriving with the enemy system in the game at the time, which was making me question the system that I had in place. The current iteration is what I made in it's place.
There are multiple components to this system:
The Behaviour Tree itself which contains all of its nodes. Setup as a scriptable object within unity so that multiple tree types can be made and placed in the editor windows of the prefabs that need an AI system.
The Behaviours which are customised as necessary per program using the system. These are the nodes that make up the Behaviour Tree. Created in the Behaviour tree scriptable object editor window from the right click context menu.
The AIBeing which is the one utilising the Behaviour tree.Â
The Pathing system utilised by the AI which is a spline based system that utilises Root Motion Animation.
The AIManager which keeps track of all the AI in the system.
The component of the program being made that needs AI, be it an NPC/Humanoid or not. It is essentially the class that is the link between the animation, the pathing and the behaviour tree. Upon creation it notifies the AIManager of its existence and then creates its Intelligence. Which is comprised of its Optics, its Knowledge and a Behaviour Manager.
Optics: Using observation settings (which are exposed in the editor) and communicating with the AIManager if it can see other Beings within the system.
Knowledge: Keeps track of recently seen beings so it doesn't have to re-poll the manager each frame.
Behaviour Manager: The part of the Being's system that processes through the behaviour tree. It unpacks the tree made in the Editor UI and allows the decisions be made by the AI.