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

Question Setting Transition Duration at runtime

Discussion in 'Animation' started by sstrong, Feb 13, 2021.

  1. sstrong

    sstrong

    Joined:
    Oct 16, 2013
    Posts:
    2,229
    I'd like to be able to update a Transition duration at runtime (not in the editor).

    Sure, there might be other tools in the Asset Store with their own animation systems, but this scenario applies to the in-built Animator.

    I can get the (read-only struct) using:
    Code (CSharp):
    1. AnimatorTransitionInfo trInfo = defaultAnimator.GetAnimatorTransitionInfo(0);
    However, I'd like to update the transition details directly or via reflection.

    I could also write a custom StateMachineBehaviour and attach it to the Transitions. However, that also AFAIK only provides read-only access.

    I thought I might be able to call CrossFadeInFixedTime in a StateMachineBehaviour but this prevented my other animations from running (maybe I'm using it incorrectly).
    Code (CSharp):
    1. public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller)
    2. {
    3.     base.OnStateEnter(animator, stateInfo, layerIndex, controller);
    4.  
    5.     controller.CrossFadeInFixedTime(stateInfo.fullPathHash, transitionDuration, layerIndex);
    6. }
    For example, I have 3 Blend Trees and transition between them. I'd like to be able to control the Transition Duration at runtime.
     
    Last edited: Feb 15, 2021
    Regretful123 likes this.
  2. Regretful123

    Regretful123

    Joined:
    Nov 5, 2014
    Posts:
    18
    I am shock and appall to see that this thread was left unanswered for almost an year now. I would like to know how I can modify the transition duration at runtime as well. I need to modify this property prior loading/playing the animations.

    Guess I'll have to research for another solution for this!
     
  3. unity_2cdeity

    unity_2cdeity

    Joined:
    Dec 19, 2020
    Posts:
    17
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,555
    You can't modify transitions at runtime. Those classes linked by @unity_2cdeity are both in the UnityEditor namespace. That's just one of the limitations you have to deal with if you use Animator Controllers.
     
  5. Regretful123

    Regretful123

    Joined:
    Nov 5, 2014
    Posts:
    18
    Is there a better approach to this? I need to be able to control what animation should play next, and how fast should the animation interpolate between?
     
  6. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,555
    animator.CrossFade or CrossFadeInFixedTime let you fade to any state as long as you don't mind hard coding the fade duration in your script where it can't be easily accessed by non-programmers and can't be previewed.

    There are also other animation systems that support a better approach to this problem but I can't tell you about them because advertising isn't allowed here (except in signatures).
     
    Regretful123 likes this.
  7. Hiep_Cao

    Hiep_Cao

    Joined:
    Sep 9, 2023
    Posts:
    1
    It's funny how
    animator.GetAnimatorTransitionInfo(LayerIndex).duration still has no setter in 2024.