Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Mecanim blend trees using the legacy animation system

Discussion in 'Animation' started by Shredsauce, Jul 16, 2016.

  1. Shredsauce

    Shredsauce

    Joined:
    Apr 4, 2016
    Posts:
    37
    I have a skiing game that uses 3 different poses for turning. Left, neutral and right. It is set up in Mecanim using a blend tree with the Horizontal axis as a parameter. The value is lerped so that the movement is smooth.

    I'm wondering if there is a way to achieve blend trees using the legacy animation system. I want to have runtime animations in and Mecanim doesn't support that yet.
     
    Last edited: Jul 20, 2016
  2. theANMATOR2b

    theANMATOR2b

    Joined:
    Jul 12, 2014
    Posts:
    7,790
    Are you sure mecanim doesnt support runtime animations?
    What purpose does runtime animations serve with this simple setup?
     
  3. Shredsauce

    Shredsauce

    Joined:
    Apr 4, 2016
    Posts:
    37
    Positive. AnimationClip.SetCurve acts as an editor function when used with Mecanim. However, it's supposed to be addressed in the near future. http://forum.unity3d.com/conversati...tcurve-work-at-runtime.462435/#message-618481

    I didn't explain it very well, so apologies for that. I want to have runtime animations for something else unrelated to the turning animations. Rather than mix Mecanim with the legacy animation system (which is messy), I figured I would convert everything else back to legacy as well.

    I ended up manipulating the blend weights of each animation individually instead of using Crossfade because it doesn't blend multiple AnimtionStates. It may not be the best way to do it but it works the way I want it to with very little overhead.

    I have an extension method that increments/decrements a float. The float is clamped between 0 and 1
    Code (CSharp):
    1.  
    2. publicstaticfloatFade01(thisfloattheFloat,floatfadeSpeed){
    3. [INDENT]theFloat+=fadeSpeed*Time.deltaTime;
    4. returnMathf.Clamp01(theFloat);[/INDENT]
    5. }
    6.  
    Normalized floats used for blending the different blend trees go up and down based on booleans
    Code (CSharp):
    1.  
    2. if(isGrounded)
    3. [INDENT]groundedMult=groundedMult.Fade01(fadeSpeed);[/INDENT]
    4. else
    5. [INDENT]groundedMult=groundedMult.Fade01(-fadeSpeed);[/INDENT]
    6.  
    The values are then used to blend the BlendTrees together. These two are opposites, so when you crouch/uncrouch the two will blend together at the speed of fadeSpeed
    Code (CSharp):
    1.  
    2. BlendTree(horizontal, (groundedMult*crouchMult*forwardMult),crouchTurnLeftAnim,crouchAnim,crouchTurnRightAnim);
    3. BlendTree(spin,(groundedMult*(1-crouchMult)*forwardMult),nuetralTurnLeftAnim,nuetralAnim,nuetralTurnRightAnim);
    4.  
    Code (CSharp):
    1.  
    2. private void BlendTree (float p_value, float p_mult, AnimationState p_left, AnimationState p_middle, AnimationState p_right) {
    3.         if (p_mult < 0.01) {
    4.             p_left.weight = 0;
    5.             p_middle.weight = 0;
    6.             p_right.weight = 0;
    7.             return;
    8.         }
    9.  
    10.         p_left.weight = Mathf.Clamp01(Mathf.InverseLerp(0, -1, p_value));
    11.         p_right.weight = Mathf.Clamp01(Mathf.InverseLerp(0, 1, p_value));
    12.         p_middle.weight = Mathf.Clamp01(1 - (p_left.weight + p_right.weight));
    13.  
    14.         p_left.weight *= p_mult;
    15.         p_right.weight *= p_mult;
    16.         p_middle.weight *= p_mult;
    17.     }
     
    Last edited: Jul 20, 2016
    theANMATOR2b likes this.
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    FWIW, before Mecanim I used SAGE for animation state machines and blend trees in the legacy animation system. SAGE is a really great tool. The only reasons why I switched to Mecanim were for root motion and humanoid retargeting.

    This may not be relevant since it sounds like you already came up with a solution, but for runtime animations in Mecanim people usually use an AnimatorOverrideController.
     
    theANMATOR2b likes this.
  5. Shredsauce

    Shredsauce

    Joined:
    Apr 4, 2016
    Posts:
    37
    Thanks, I'll check out SAGE! I can swap animations in Mecanim. What I'm looking to do is create them at runtime.

    Also, I just realized I've been misspelling Mecanim this entire time.