BehaviorTree
BehaviorTree Editor
The editor is used to edit a behavior tree(BT) asset. The editor can be opened via doucble clicking an BT asset
or via Window->BehaviorTreeEditor. You can load, save or save as a copy of an asset from the file menu located in the editor.
New nodes can be creted via the create node menu, each new node is created from a base template and more templates can be added for quicker node creation, the default nodes that can be created
is base node, task, decorator, service, utility action and utility consideration.
Creating a new tree or blackbaord can be found in the unity create asset menu Create->AI->BehaviorTree|Blackbaord.
Features
- Save, Save As, Load Behavior Tree Asset
- Create new nodes
- Minimap
- Inspector for selected node
- Inspector for blackboard to be used
- Access Blackboard keys via nodes
- Utility AI Nodes
- Realtime node updates
- Breakpoints - via node context menu
- New nodes can be created using the search menu, spacebar or the graph context menu
Project Requirements:
Blackboard
The blackboard asset allows the user to add keys that can be access via the graph. The blackboard uses a dictionary to store keys so each key
must have an unique name to be serilaized to the asset.
BlackboardTreeComponent
To use the behavior tree asset a gameobject needs the behavior tree component and select a BT asset the component will create an instance of.
Default Nodes
Each nodes displayed name and description can be edit via the inspector.
- Root- Created by default when opening an BT asset for the first time, all trees use this as the top most node
- Composites:
- Selector - Fails if all nodes fail and is a success if one node is a success, stops executing when on child succeeds
- Sequence - Stops executing when one child fails. if a child fails the sequence fails, otherwise success when all childen succeeds.
- Decorators:
- Is At Location
- Tolerance - acceptable distance from the location to succeed
- Destination
- Loop - Loops child
- Repeats - Number of times to loop, -1 = inifintly
- Loop Until Fail - Loops child inifintly until the child fails
- Cooldown - Loacks execution of the child until the timer expires
- Abort Type
- Timer
- Interval - the time it takes to expire
- Deviation - range of time from the interval that determines time it takes to expire
- Force Success - forces a child to return success
- Inverter - inverts the returned state from the child
- BlackboardbaseCondition - determines if a key is set or not
- Abort Type
- Query - Is Set or not set
- Key
- Tasks:
- Log Message - logs to the unity debug console
- MoveTo - move the agent to a destination
- Speed
- Stopping distance
- Update Rotation
- Acceleration
- Tolerance
- Destination Key
- Wait- waits an amount of time before finishing executing
- Run behavior - runs a sub behavior tree for more complex behaviors
- Random Location - finds a random location on the navmesh to set the destionation to in certain radius from agent
- Action Seletor - selects an action if the action score is the highest
- Action - takes to nodes the first takses in multiple considerations, the second takes a node to be executed if the action is selected to be executed
API and Custom Nodes
The nodes that can extended include the follwing:
- Decorators
- Services
- Conditional Abort
- Task
- UtilityAI
- consideration
The follwing methods can be overriden:
Base Node, Decorator, Task:
protected override void OnStart() {}
protected override void OnBeginExecute(NavMeshAgent agent) {}
protected override ENodeState OnExecute(NavMeshAgent agent){}
protected override void OnEndExecute() {}
protected override void OnAbort() {}
Conditioanl Abort:
public abstract bool OnConditionCheck(){}
protected virtual void OnObserverBegin() {}
protected virtual void OnObserverEnd() {}
Consideration:
protected override float OnEvaluateScore(NavMeshAgent agent){}
Service:
protected override void OnServiceTick(){}
Misc Classes
BlackboardKeySelector - a utility class that can be used to access the keys on a current blackboard asset set in the behavior Tree