Search Unity

Legacy Animation - Unscaled time?

Discussion in 'Animation' started by VoodooDetective, May 5, 2021.

  1. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    191
    How do I make legacy animations play in unscaled time?
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    1,801
    Does the Legacy Animation component have a method like Animator.Update which lets you specify the delta time?
     
  3. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    191
    This is what I'm able to do currently, but it feels hacky and ?inefficient?

    Code (CSharp):
    1.         void Update()
    2.         {
    3.             if (animator.isPlaying == false || string.IsNullOrEmpty(currentAnimation)) return;
    4.             AnimationState currentState = animator[currentAnimation];
    5.             currentState.time += Time.unscaledDeltaTime;
    6.             animator.Sample();
    7.         }

    I thought for sure I was missing something. Also, I'm not entirely sure how this works. I assume it respects the frame-rate of the animation, but I'm not 1000000% positive.
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    1,801
    Yeah, that looks rather hacky but I can't see a better way to do it.

    Why are you using the legacy animation system though? If it's just to avoid Animator Controllers, you might be interested in Animancer (link in my signature) which can be set to use unscaled time without needing any code.
     
  5. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    191
    I actually did buy and love Animancer! The problem is I need the values set by the animator to be retained after the animation completes, and I haven't found a way to make that happen with Animator or Animancer.

    With Animator, even if I set "don't write defaults," the animator ignores if I set an object inactive on the last frame.

    Do you know anyway to make any of the three options work?
     
  6. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    1,801
    If you just leave the animation playing it would stay at the values of the last frame so I'm not really sure what the issue is, but with Animancer you can make it stop doing anything by calling animancer.Playable.PauseGraph(); if you want to prevent it from wasting any performance re-applying the same values every frame.
     
  7. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    191
    Ah so I think I didn't communicate that clearly. I need the changes the animator makes to be persistent, even after the animation has stopped playing.

    Example:
    animator.Play(ShowMainMenu);
    animator.Play(ShowModal);

    If I do this, main menu will disappear when I show the modal, but I don't want it to disappear.
     
  8. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    1,801
    If you play each animation on a separate Layer then they won't interfere with each other so you can have them all playing at once. I prefer to use a tweening system instead of animations for UI stuff because of issues like this so I'm not certain it will fix the problem, but it's worth trying.
     
  9. VoodooDetective

    VoodooDetective

    Joined:
    Oct 11, 2019
    Posts:
    191
    Ahhh genius! Thanks so much for the suggestion. I feel much better not using my hack. Thank you!
     
unityunity