How to run two animations at once? This is one of the most common problem in the animations field. Let’s see how Unity can help us to solve this problem.
Let’s assume this situations: You have a 2d character that has a run animation and a run-attack animation, and you want to make the character be able to run and attack at the same time. Now the simple solution is to play the run animation when when player start to run, and switch to the run-attack animation when the player press the attack button, the problem is that the transition between the run and the run-attack animation is not a smooth transition. The problem can be solved with 2 solutions:
The easy one is to use the Animation.Blend method provided by unity:
public void Blend(string animation, float targetWeight = 1.0F, float fadeLength = 0.3F);
What does this method do?
It Blends the animation named animation towards targetWeight over the next time seconds. Basically is a smooth way to go from one animation to another one.
Run two animation at once.
But what if you have 2 very complex animations which are very difficult to synchronize? the best solution is divide the 2 animations into 3 animations:
- One is the animation used to animate the legs.
- Another is the animation used to animate the torso.
- Another is the animation used to animate the torso while the attack is occurring
Once we have the animations, when the player press the button to attack while the character is running, only the the torso animation will be switched, and to make the transition even more smooth, we’ll use the Animation.Blend method to change from the run animation of the torso to the run-attack animation of the torso.
Let see how to use Unity to achieve the result that we want:
First thing to do Is to create the Avatar to use. Let’s create a Hero character with a rigidbody attached:
Has you can see, our hero has one child gameObject named Body where we’ll use to attach the animations of the body, remember to attach a sprite rendere to the body if your animation is sprite based:
Now Import your animations sheet:
As you can see, we have 3 animation, one for the legs and 2 for the body..
Create the animations:
Now let’s add the animations to our character:
Select the legs and drag the frames into the player gameobject to automatically create an animation clip:
Once done, an animation controller along with an animation clip will have been created:
Now create the animations for the body, open the animation windows –> create new clip and select the body gameobject sprite rendere component:
Now add the animations frame:
Do the same thing for all the animations that needs to be played on the upper part of the character:
Setting the animator:
Now open up the animator windows and create 2 states: Idle and run and a trigger that will switch from the Idle to the run state:
If we trigger the Playr_run condition, our animation will go from the Idel state to the Run state, but this will only show the lower part of the animation, now we have to add another Layer to our animator, the new layer will allow us to add another animation that will run simultaneously with the legs animation.
Go to Layer under the animator window and click on the plus button to add a new layer:
Click on the gears icon and set is as shown in the following picture:
- The weight is the priority of the animations A higher layer number has a higher priority.
- The sync property will create an copy of our first layer of the animator but with the advantages that on this layer, we can choose to run different animation. Go to the run animation on the layer that we’ve just created and set the animation clip of the animation to the one relative to the upper part of the body:
We have just create a animator with 2 layers that will do exactly the same, but on one layer we’ll run the legs animation, and on the other the body animation.
Now the only thing that you have to do, is to trigger the animations in code:
Here I gave you the idea of how to run two animations at once.
This base example can be easily expandable to run different animations or even more then 2 animations at once.