Merge 2 animations?

Hello,

I try to make a third person game in which you control this character:
Warrior

He uses 3 animations: idle,run,attack

The problem is that as my character is walking, he plays the “run” animation.
As soon as I click the corresponded button for the attack, he plays the “attack” animation.

However, on the “attack” animation the legs are not moving and it is odd.

Is there any way I can continue to play the “run” animation only on his legs as the upper body part plays the “attack” animation?

Thanks in advance!

Yes, you can do this in both the legacy and Mecanim animation systems.

In Legacy, you’ll want to leave “idle” and “run” on layer 0, and put “attack” on layer 1. The “attack” animation on layer 1 will only affect the upper body, using what’s called a mixing transform. Script Reference. For example:

//(Based on the example in the script reference above.)

function Start () {
   // Set all animations to loop
   animation.wrapMode = WrapMode.Loop;
   // except attack
   animation["attack"].wrapMode = WrapMode.Once;

   // Put idle and walk into lower layers (The default layer is always 0)
   // This will do two things
   // - Since attack and idle/run are in different layers they will not affect
   //   each other's playback when calling CrossFade.
   // - Since attack is in a higher layer, the animation will replace idle/walk
   //   animations when faded in.
   animation["attack"].layer = 1;

   // Adds a mixing transform so attack uses only upper body:
   var mixTransform : Transform = transform.Find("root/hips/spine2");
   animation["attack"].AddMixingTransform(mixTransform);

   // Stop animations that are already playing
   //(In case user forgot to disable play automatically)
   animation.Stop();
}

function Update () {
   // Based on the key that is pressed,
   // play the walk animation or the idle animation
   if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.1)
      animation.CrossFade("run");
   else
      animation.CrossFade("idle");

   // Shoot
   if (Input.GetButtonDown ("Fire1"))
      animation.CrossFade("attack");
} 

In Mecanim, you’ll do something similar, but it’s easier. You’ll once again use layers. Put idle and run on Layer 0, and attack on Layer 1 with an avatar body mask for the upper body only.