-
Notifications
You must be signed in to change notification settings - Fork 5
Animation Transitions
Animation transitions allow the state machine to switch or blend from one animation state to another. Transitions define not only how long the blend between states should take, but also under what conditions they should activate. You can set up a transition to occur only when certain conditions are true. To set up these conditions, specify values of parameters in the Animator Controller.
For example, your character might have a "patrolling" state and a "sleeping" state. You could set the transition between patrolling and sleeping to occur only when an "alertness" parameter value drops below a certain level.
// Create the controller and the parameters
AnimatorController animator = new AnimatorController(animComposer);
animator.addParameter("isRunning", AnimatorControllerParameterType.Bool);
animator.addParameter("isReloading", AnimatorControllerParameterType.Trigger);
player.addControl(animator);
// Define states for animations.
AnimatorControllerLayer layer0 = animator.getLayer(AnimComposer.DEFAULT_LAYER);
AnimatorStateMachine sm = layer0.getStateMachine();
AnimatorState idle = sm.addState("Idle", AnimDefs.RifleIdle);
AnimatorState walk = sm.addState("Run", AnimDefs.RifleRun);
AnimatorState reload = sm.addState("Reload", AnimDefs.Reloading);
AnimatorStateTransition idleToWalk = idle.addTransition(walk);
idleToWalk.addCondition(AnimatorConditionMode.If, 0f, "isRunning");
AnimatorStateTransition walkToIdle = walk.addTransition(idle);
walkToIdle.addCondition(AnimatorConditionMode.IfNot, 0f, "isRunning");
AnimatorStateTransition idleToReload = idle.addTransition(reload);
idleToReload.addCondition(AnimatorConditionMode.If, 0f, "isReloading");
// execute 95% of the reloading animation before returning to idle state
AnimatorStateTransition reloadToIdle = reload.addTransition(idle, 0.95f);
// set the initial state.
sm.setDefaultState(idle);
Use the following properties to adjust the transition and how it blends between the current and next state.
Property | Function |
---|---|
offset | The time at which the destination state will start. Default is 0 |
duration | The duration of the transition. Default is 0.25 |
exitTime | If hasExitTime is true, this value represents the exact time at which the transition can take effect. This is represented in normalized time (for example, an exit time of 0.75 means that on the first frame where 75% of the animation has played, the Exit Time condition is true). |
hasExitTime | When active the transition will have an exit time condition. |
mute | Mutes the transition. The transition will never occur. |
destinationState | The destination state of the transition. |
conditions | Conditions that need to be met for a transition to happen. |
A transition can have a single condition, multiple conditions, or no conditions at all. If your transition has no conditions, the API only considers the exitTime
, and the transition occurs when the exit time is reached. If your transition has one or more conditions, the conditions must all be met before the transition is triggered.
A condition consists of:
- An event parameter (the value considered in the condition).
- A conditional predicate (if needed,for example, ‘less than’ or ‘greater than’ for floats).
- A parameter value (if needed).
If hasExitTime
is enabled for the transition and has one or more conditions, these conditions are only checked after the exit time of the state. This allows you to ensure that your transition only occurs during a certain portion of the animation.