• Unity
  • Blend animations using Unity Runtime

Hey there,

Is it possible to blend animations in Unity using the AnimationState class? I'm not referring to mixing, which as I understand it allows you to transition from one animation to another over time. What I'm looking for is a way to blend animations, so that I could say mix 50% of a walk animation with 50% of a run animation to get a "jog".

In the main Spine Documentation I found the Mix method that contains an Alpha parameter and sounds like what I'm looking for. But will I have to make my own system to use it, or is there some way to get it to work with AnimationState?

Thanks!

Related Discussions
...

Using the AnimationState class?
I haven't tried it, but I looked through the code briefly and it should be possible.

Considering:
(1) AnimationState has a track system. If you call SetAnimation(0, walk, true); then SetAnimation(1, shoot, true); then both walking and shooting animations will play. walk animation pose gets applied first. Then then shoot animation is applied on top of it.

(2) Each entry on the track system (see the Spine.AnimationState.TrackEntry class) has controllable properties like TimeScale, Time and Mix.

(3) TrackEntry.Mix in particular isn't about crossfading between one animation to another. It's actually an alpha/percentage of the pose that will get applied to the skeleton. the Mix property affects the alpha parameter that gets used by AnimationState internally when it calls Animation.Apply, or in this case, Animation.Mix.

So. Theoretically, the code is:

Spine.AnimationState state = skeletonAnimation.state;
state.SetAnimation(0, walk, true); // walk on track 0.
var track = state.SetAnimation(1, run, true); // run on track 1.
track.Mix = 0.5f;

21 Mar 2016 2:52 am


Tested it with Spineboy's walk and run.
This code works. Put it in your project and test it out:

using UnityEngine;
using Spine;

public class AnimationMixerTest : MonoBehaviour {
   [Range(0f, 1f)]
   public float mix = 0.5f;
   Spine.TrackEntry track;

   void Start () {
      var state = GetComponent<SkeletonAnimation>().state;
      state.SetAnimation(0, "walk", true);
      track = state.SetAnimation(1, "run", true);
   }

   void Update () {
      if (track != null)
         track.Mix = this.mix;

   }
}

All the same, it will have to be your system that manages increasing and decreasing the mix, and fading it out/clearing the track when it needs to be cleared.

This is perfect, thank you! Exactly what I needed. Really appreciate your help.