Search Unity

Blending 2 animations with a blend tree

Discussion in 'Animation' started by enblomquist, Nov 15, 2018.

  1. enblomquist

    enblomquist

    Joined:
    Jul 25, 2012
    Posts:
    85
    Hi -

    We are attempting to blend two animations by playing back their frames by using the mouse using the Blend tree in Mecanim.

    Essentially, by moving the mouse, the sword should move in relation to position on the screen.

    Here are the two 'animations' we are trying to blend together. (separated in Unity)



    We've had success in setting this up...technically. We can see the numbers blending properly in the Blend Tree. But the animations aren't firing off properly. Mostly inconsistent / erratic behaviors.

    Here is our Blend Tree setup:



    Here is our current code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace RocketCat.DBTS.Dad.Sword
    6. {
    7.     public class DadArmAnimator : MonoBehaviour
    8.     {
    9.         [Header("Object References")]
    10.         public Animator ArmAnimator;
    11.  
    12.         [Header("Animation Parameters")]
    13.         public string AnimParam_SwordDrawn;
    14.         public string AnimParam_MoveSwordHoriz;
    15.         public string AnimParam_MoveSwordVert;
    16.  
    17.         private Vector2 CurrentSwingMomentum = Vector2.zero;
    18.  
    19.         void Start()
    20.         {
    21.             DadValues.RUNTIME_ArmAnimator = this;
    22.         }
    23.  
    24.         public void BeginArmSwinging()
    25.         {
    26.             ArmAnimator.SetBool(AnimParam_SwordDrawn, true);
    27.  
    28.             CurrentSwingMomentum.x = 0.0f;
    29.             CurrentSwingMomentum.y = 0.0f;
    30.  
    31.             ArmAnimator.SetFloat(AnimParam_MoveSwordHoriz, CurrentSwingMomentum.x);
    32.             ArmAnimator.SetFloat(AnimParam_MoveSwordVert, CurrentSwingMomentum.y);
    33.         }
    34.  
    35.         public void EndArmSwinging()
    36.         {
    37.             ArmAnimator.SetBool(AnimParam_SwordDrawn, false);
    38.         }
    39.  
    40.         public void HorizontalMovement(float inMovement)
    41.         {
    42.             inMovement *= DadValues.SETTING_SwordSwingSpeedState;
    43.             CurrentSwingMomentum.x = Mathf.Clamp(CurrentSwingMomentum.x + inMovement, 0f, 1f);
    44.             ArmAnimator.SetFloat(AnimParam_MoveSwordHoriz, CurrentSwingMomentum.x);
    45.         }
    46.  
    47.         public void VerticalMovement(float inMovement)
    48.         {
    49.             inMovement *= DadValues.SETTING_SwordSwingSpeedState;
    50.             CurrentSwingMomentum.y = Mathf.Clamp(CurrentSwingMomentum.y + inMovement, 0f, 1f);
    51.             ArmAnimator.SetFloat(AnimParam_MoveSwordVert, CurrentSwingMomentum.y);
    52.         }
    53.     }
    54. }
    Thanks!
     

    Attached Files:

  2. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    Perhaps I'm just not seeing it but are you actually calling the horizontal and vertical movement methods anywhere?

    Also just a quick tip. If you want to clamp something between 0 and 1 you can use Mathf.Clamp01 instead.

    CurrentSwingMomentum.x = Mathf.Clamp(CurrentSwingMomentum.x + inMovement, 0f, 1f);
    becomes
    CurrentSwingMomentum.x = Mathf.Clamp01(CurrentSwingMomentum.x + inMovement);
     
  3. enblomquist

    enblomquist

    Joined:
    Jul 25, 2012
    Posts:
    85
    Hi thank you -

    Yes those methods get called by the input system. We can prove that because the values change when you move the mouse...
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    It sounds like you want the animations to not actually be playing, just paused at a specific time proportional to the parameters. But I don't think Blend Trees can actually do that. They play the animations constantly and blend their weights instead.

    If you really want to use animations, my Animancer plugin would let you set the speed/time/whatever as you like, but honestly I think the best approach for this problem would be to just script it yourself. Once you pick which angles on each axis correspond to each side of the screen, interpolating between them and turning the result into a Quaternion to give the bone would be pretty simple.