Saturday, December 17, 2016

Control an Animation Transition

This post covers transitioning an animation with a software event.  This post builds upon Importing an Animation into Unity.  The example below is based on having imported a model from Blender that already has two animations embedded in it (swim and idle).

This post will show how to setup a transition condition, and how to trigger it in code.

Looking first at the Animator Controller view, you'll see I've already added transitions between the Swim and Idle state.




In the upper left hand corner, I added a Bool parameter labeled "Swim".  This will be used as the condition by which we transition between the states.


With the down arrow selected, the Inspector window shows details of the transition.


First I uncheck the "Has Exit Time" box.  This allows the destination state to play without waiting for the current state to finish.  Next I set the condition to be when my Bool Swim goes false.


Clicking on the transition from Idle to Swim, shows that I set it up the same way except that this transition occurs when the Swim bool goes true.


Next I write a script that sets this parameter based on a time duration.  This could be (more appropriately) done based on a trigger event or controller input rather than a timer.

Note that the Bool Swim is access through the SetBool( ) function.


Finally, add the script to the object with the Animator and add the Animator to the appropriate field in the script parameters.


At this point when the game is played the animation should switch states every 5 seconds.

No comments:

Post a Comment