Setting Transition Duration at runtime

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: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).

public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex, AnimatorControllerPlayable controller)
{
    base.OnStateEnter(animator, stateInfo, layerIndex, controller);

    controller.CrossFadeInFixedTime(stateInfo.fullPathHash, transitionDuration, layerIndex);
}

For example, I have 3 Blend Trees and transition between them. I’d like to be able to control the Transition Duration at runtime.

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!

i dont believe you can use StateMachineBehaviour to access the class, may need to access the AnimatorControllerLayer/AnimatorController to make it at runtime. (not my specialty thou)

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.

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?

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).

It’s funny how
animator.GetAnimatorTransitionInfo(LayerIndex).duration still has no setter in 2024.