Search Unity

Change the transition duration between two animation by scripting

Discussion in 'Scripting' started by herbie, Apr 16, 2016.

  1. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Hi,

    I have a question about the transition duration in mecanim.
    Is it possible to change the transition duration between two animations by scripting?

    Something like animator.transitionDuration("animation1", "animation2", 0);
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  3. herbie

    herbie

    Joined:
    Feb 11, 2012
    Posts:
    237
    Ok, I will have a look at it. Thanks.
     
  4. koby234

    koby234

    Joined:
    Aug 15, 2018
    Posts:
    1

    Even though this is 2 years late there still isn't a full answer so I'll post my solution.
    After some research i found some info and changed the script here so it will work in unity https://gamedev.stackexchange.com/q...ansition-speed-between-2-animations-during-ru

    Paramaters info:
    string stateName -
    The name of the state the transition is coming from
    string transitionName - The name of the transition
    int layerIndex - The index of the layer the transition is on (if you have never used extra layers this value is 0)
    float desiredDuration - The new value for the duration of the transition
    Animator animator - The animator on which the transition exists

    Code (CSharp):
    1.  
    2. public static void ChangeTransitionDuration(string stateName, string transitionName, int layerIndex, float desiredDuration, Animator animator)
    3.     {
    4.         // Get a reference to the Animator Controller
    5.         UnityEditor.Animations.AnimatorController ac = animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController;
    6.  
    7.         int layerCount = ac.layers.Length;
    8.         if (layerCount <= layerIndex)  // Error chek
    9.         {
    10.             Debug.Log("The layer index does not exist)
    11.            return;
    12.        }
    13.  
    14.        UnityEditor.Animations.AnimatorStateMachine sm = ac.layers[layerIndex].stateMachine;
    15.        UnityEditor.Animations.ChildAnimatorState[] states = sm.states;
    16.        foreach (UnityEditor.Animations.ChildAnimatorState s in states)
    17.        {
    18.            //Debug.Log(string.Format("State: {0}", s.state.name));
    19.            if (s.state.name == stateName)
    20.            {
    21.                foreach (UnityEditor.Animations.AnimatorStateTransition t in s.state.transitions)
    22.                {
    23.                    if (t.name == transitionName) {
    24.                        Debug.Log(string.Format("Changing {0} duration value", transitionName));
    25.                        t.duration = desiredDuration;
    26.                    }
    27.                    //Debug.Log(string.Format("{0} transtion: {1}", s.state.name,t.name));
    28.                }
    29.            }
    30.        }
    31.    }
     
    jnas, Serinx and TurleyE like this.