Syncing Animations
Behavior Designer includes a set of Animator tasks which allow you to play animations within the tree. Consider the tree below:
This is an extremely simple Animator Controller that uses a blend tree to blend between the Idle and Run state based on the Speed parameter. If you’d like to then sync this blend tree with the Seek task your behavior tree would then look similar to:
Before the Seek task starts the Speed parameter is set to 1 so the blend tree can play the run animation. After the Seek task completes the Speed parameter is then set to 0 to play the Idle animation again.
With a small behavior tree and Animator Controller this process works well. However, as the amount of animations grow for your agent this method starts to become extremely cumbersome. In addition, your behavior tree also starts to become extremely verbose with all of the Animator tasks and if you change a transition within your Animator Controller you’d have to rework the behavior tree to work with that Animator Controller change.
There is a better way.
The recommended approach to syncing animations within your behavior tree is to not sync the animations at all within the behavior tree. Instead your agent’s character controller should do it for you. Using Unity’s NavMeshAgent as an example, when the behavior tree sets the NavMeshAgent destination (such as with the Seek task) the NavMeshAgent’s velocity will change to move towards the destination. The character controller should then use the velocity and translate the movements into parameters that the Animator Controller can understand. MecWarriors had an excellent tutorial on how to accomplish this. The MecWarriors site is no longer operational but a cached version of the tutorial can be found here. Unity also has a similar tutorial within their documentation. A similar implementation can be done with Apex Path and A* Pathfinding Project.
The advantage of this approach is that your behavior tree is then not aware of the animations at all and it can even work with root motion. You also don’t over-complicate your tree with Animator tasks and makes your tree a lot cleaner. The Ultimate Character Controller uses this approach and for that asset we created a pretty large behavior tree. You can see the behavior tree on this page and you’ll notice that there are no Animator tasks within the tree. A bridge component is linking the NavMeshAgent’s velocity to the character controller’s inputs which then allow the Third Person Controller to take care of the animations.