Search Unity

How to change ExitTime at runtime?

Discussion in 'Animation' started by nTu4Ka, Oct 10, 2021.

  1. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    Hello,

    I have a predefined Animator. There are different characters created at runtime that are using this basic Animator.
    Each character has specific exit time from death animation to corpse. For one it's 0.5 for another it's 0.7.
    [Idle] ---> [Death anim] ---> [Corpse anim]

    Is it possible to change specific transition exit time at runtime?

    I found a solution with UnityEditor.AnimatorController but it doesn't work in build.

    Code (CSharp):
    1.  
    2. using UnityEditor.Animations;
    3.  
    4. AnimatorController ac = DeathAnimator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController;
    5.         foreach (var LookupState in ac.layers[0].stateMachine.states) {
    6.             if (LookupState.state.name == "DeathAnim") {
    7.                 foreach(var DeathTransition in LookupState.state.transitions){
    8.                     DeathTransition.exitTime = this.DeathExitTime;
    9.                 }
    10.             }
    11.         }
    I also need to use specifically defined ExitTime since some game logic depends on it.
    I'm checking if animation reached ExitTime on specific character because it affects other characters actions.
    Having this value defined will allow mecanim alignment with game logic. Otherwise incorrectly set value by hand may result in a bug.
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Hey,

    I think it's possible, but it's not simple. You can check out this post for somewhat of an answer: https://forum.unity.com/threads/a-s...n-animation-while-button-is-held-down.520862/

    If you want a really simple solution, you can check out Animancer: https://assetstore.unity.com/packages/tools/animation/animancer-lite-116516

    It allows you to easily choose when an animation ends through code. You can try it out for free, but the pro version is paid. I've been using it for a while and it solves a lot of the problems I was facing with mecanim, so I think it's worth it, but your mileage may vary.

    Best of luck!
     
  3. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    Just thought I'd also mention an alternative solution.

    You can use
    Animator.GetCurrentAnimatorStateInfo(0).normalizedTime;
    to get the current normalized time of animation and then transition out using code at different times for each character.

    I don't recommend doing this though, as I imagine it could be a pretty big pain to set up.
     
    nTu4Ka likes this.
  4. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    Thank you!

    I also like the idea with normalizedTime.
     
    Unrighteouss likes this.
  5. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    One year later and I moved to Animancer. And I have zero regret even though I had to rewrite like 20% of the code.

    At least for my project Mecanim is hell while Animancer is heaven.


    Thank you very much for suggesting it! :)
     
    Unrighteouss likes this.