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

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.    }
     
  5. Xtro

    Xtro

    Joined:
    Apr 17, 2013
    Posts:
    604
    This answer is wrong because you can't use UnityEditor namespace in runtime. It may look it's fine in Unity Editor but as soon as you try to build the game, it will fail.

    Unfortunately, it appears that there is no way to change the transition duration in runtime and we are doomed!! :(
     
  6. ratiug36

    ratiug36

    Joined:
    Sep 18, 2015
    Posts:
    1
    Maybe a workaround is create multiple transitions between the same clips with different transition values and then use a parameter to control witch transition will be used (aimSpeedLvUpgrade == 1 or aimSpeedLvUpgrade == 2) but i don't know how viable this is in large scale

    upload_2023-7-19_8-48-10.png upload_2023-7-19_8-48-45.png