Unity : how to run two animations at once

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:

 

Animation Blend

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:

  1. One is the animation used to animate the legs.
  2. Another is the animation used to animate the torso.
  3. 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:

Unity add player

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:

Adding sprite renderer

Now Import your animations sheet:

3

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:

Create animation unity

Once done, an animation controller along with an animation clip will have been created:

Animator Unity

Now create the animations for the body, open the animation windows –> create new clip and select the body gameobject sprite rendere component:

Aniamtion clip add

Now add the animations frame:

7

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:

8

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:

9

Click on the gears icon and set is as shown in the following picture:

Setting layer animation unity

  1. The weight is the priority of the animations A higher layer number has a higher priority.
  2. 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:

set animation clip in aniamator

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:

Run animation 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.

Written By
More from Marco

Global Game Jam guide: get the most out of the ggj plus cheat sheet

The Global Game Jam (GGJ) is one of the biggest events for...
Read More

5 Comments

    • Yes, you can use this technique also for 3d models, but you don’t usually need to do so. You can use blend trees for example to blend 2 animations together. have a look at this unity section to better undestand what fits your needs

  • Two sprite based animations playing at once requires two Animators, right? Or how could one sprite renderer display sprites from two animations at once? As you describe it, it looks like it would be two layers in one animator, which renders two sprite animations, but I have not found any way of doing this (either the legs or the upper body in your example would be renderer, not both at the same time).

Leave a Reply

Your email address will not be published. Required fields are marked *