- Home /
Interrupting animations
I am making 2D top-down view game and I would like the player to be able to cancel certain animations while they are playing.
Let's assume I have an idle animation and a simple animation of swinging a sword. Idle animation does nothing but sets the sword rotation to 0 degrees. Swinging animation rotates the sword from 0 to 90 degrees.
I would like to be able to cancel the sword swing animation at any time. The problem is for smooth transition between animations I would need to start the backswing animation with the same rotation of sword as in the swing animation at the moment when it was interrupted.
So I've prepared backswing animation which was mirrored swing animation: it rotates sword from 90 to 0 degrees.
I've tried using blending tree to blend between those two animations and it worked well unless the input which cancelled animation occured after half of swing animation time: the backswing animation only played for the remaining half of animation time and it ended before reaching the 0 degree rotation, which caused sword to jump.
I thought of starting the backswing animation at frame in which sword rotation is the same as it was when the swing animation was interrupted, but for this I would need to be able to set transition offset of backswing animation dynamically, depending on the moment of interrupting swing animation.
So my question is: Is there any way to set the offset of transition dynamically by script? Or is there any other way to achieve smooth transition if the starting position of next animation needs to be based on interrupted frame of current animation?
Answer by WujekEvil · Aug 16, 2015 at 06:43 PM
Seems like I found out a solution I needed. Attaching following behaviour script to swing animation makes the animation transition smooth and starting from desired rotation:
// OnStateUpdate is called on each Update frame between OnStateEnter and OnStateExit callbacks
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
//"Block" is my trigger for interrupting weapon swing animation
if(animator.GetBool("Block")==true)
{
//the 4th parameter is the offset of transition
animator.CrossFade("Cancel", 0f, layerIndex, 1f-stateInfo.normalizedTime);
animator.SetBool("Block", false);
}
}
Your answer
Follow this Question
Related Questions
One animation breaks playing another 1 Answer
Mecanim Root Motion Rotation - 180 degree turn. 2 Answers
Mecanim transition between animation help 0 Answers
Combining two animations to be one 0 Answers
How can i jump my character immediately With Mecanim 3 Answers