Lately I have been looking into behavior trees and how to implement them to my enemy’s AI but I cannot find anything that is a complete solid tutorial that explains how the code works and how to use it. I want to create an easy to use tree that doesn’t get too overwhelming as I add to it.
I found this, it is a very good example of what I am trying to achieve. Any help would be very much appreciated.
EDIT
I would also like to say that I do not want to use pre-built Behavior Trees on the asset store, that kinda defeats the purpose of learning how to make one.
Behavior Trees can be quite complex a solid step by step tutorial may not exist for it. There are, however, several posts that have them implemented and try to explain each piece for your understanding. Just google “implementing behavior tree” and don’t be afraid to try to convert code from other languages (Which is a great learning experience).
A comment on your link, trying on your own to implement a concept such as the BT described in that post, in my experience, is a better method for learning how to create something rather than writing a line of code just because a video said to. You also get a deeper understanding of why something is done the way it is.
Examining code created by another person is one of the best ways to learn. You learn different programming methods and techniques for solving problems.
I will try this. Do you know of any free packages I can try to dissect? I tried downloading the Panda BT asset, but I cant find any actual code, just editor info and executable s.
I personally do not know of any open-source packages on the Asset Store, but there are quite a few open-source repositories on the web. There are a few in C# and most likely much more in other languages.
Also, don’t think that you need to write a visual editor. That’s a convenience, kind of like how PlayMaker is a convenience for writing FSMs in code. Back in the day, we defined behavior trees like this:
var tree = new BehaviorTree(
new Selector(
new Sequence(
new Inverter(
new CanSee("Player")),
new Wander()), // Can't see player, so wander.
new Sequence(
new InShootingRange("Player"),
new ShootAt("Player")), // In range, so shoot at player.
new Sequence(
new Seek("Player")))); // Otherwise close to range.
Of course, convenience or not, a graphical view is really handy, especially at runtime, to let you know the current state of the tree.
A quick google search turned up this tutorial in JavaScript that looks like it builds trees in code.
I’m the author of Panda BT (www.pandabehaviour.com). If you have any specific question about implementing your own BT system, I’d be glad to answer.
The implementation of the core of a BT system is not a big deal and rather simple once your understand how a BT work. The most challenging part is to turn it into a usable tool and integrate it within Unity.
For instance, it is really cumbersome and very verbose to define the tree directly in C#. That’s why, in Panda BT, I’ve decided that the tree is defined into an external script (a TextAsset) using a minimalist scripting language. For illustration the same BT as @TonyLi will be translated to:
Having your tree defined externally has the avantage to make it easier to visualize the tree as it is running. This would have been really hard or nearly impossible to do this if the BT is defined in C#.
Note that Panda BT is not a collection of ready to use AIs, but it is simply a framework: you still need to write your own code to implement your AI. I would like to highlight that there’s a difference between implementing a BT system and implementing an AI using a BT system.
If you want to see some code beyond the examples already given, the Gamasutra article you link to has a link to a Java BT framework that the author is using. It’s not the way I would code things, but it will give you another broad idea of what’s involved to implement your own behavior tree system.