Use Behavior Tree in Unity
In the easiest way, bring everyone to know and use the behavior tree, this series is updated for a long time, I hope everyone likes it!
Your support is my motivation for writing.
table of Contents:
1. Create a behavior tree for GameObject
2. Introduction to the Behavior Tree Work Panel
3. Say Hello to the behavior tree
4. Use variables in the behavior tree
5. Combining nodes Squence and Selector
There are many behavior tree plugins in Unity, such as Rain AI, Behavior Designer, Bolt, etc.
Here we use the most acclaimed Behavior Designer.
1. Create a behavior tree for GameObject
Just add this component to GameObject.
Note that there are several parameters:
- Behavior Name: the name of the behavior tree
- Behavior Description: Behavior tree description
- Variables: display the variable area of the behavior tree
- Options: Behavior tree settings
- Start When Enable: This object starts to execute once it is enabled
- Pause When Disable: Pause the behavior tree when this object is disabled
- Restart When Complet: Reexecute the behavior tree after the behavior tree is executed
2. Introduction to the Behavior Tree Work Panel
From left to right:
Behavior trees, nodes, variables, attributes
Lock: Lock
Export: export
3. Say Hello to the behavior tree
Node analysis:
Node name | classification | effect |
Entry | Behavior Tree Entry Point | |
Sequence | Composite node | Execute child nodes in turn until they fail |
Log | Behavior node | Output debug messages |
Note: After selecting the node, select the properties panel to see the properties of the node.
For example, the properties of the Log node are as follows:
The description is equivalent to a comment, which will be displayed.
4. Use variables in the behavior tree
If what we want to say changes, we need a variable to store our content.
Create variables:
In the Variable panel, we can enter the variable name, set the type, press Enter, and the sauce ~ a variable will be created.
We can modify the value in the working view or in the properties panel. Now we can see our variables externally.
Use variables:
Click the dot to set the variable
Here we can talk to the behavior tree as we please.
It is worth mentioning that what we create in this way is a local variable that can only be used in this behavior tree. (Whispering to only one person)
We can also create global variables so that all behavior trees can be heard.
5. Combining nodes Squence and Selector
Squence: The child nodes are executed in sequence until they fail
Selector: The child nodes are executed in sequence until they succeed
Result output: ABC, return true after the execution of the behavior tree
Result output: A, return false after the execution of the behavior tree
You may wish to try to use the two combination nodes to have any effect?
If you have any questions, you can leave a message.