Search Unity

Animation freezes with negative speed

Discussion in 'Animation' started by andyz, Jun 1, 2015.

  1. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,277
    In unity answers this seems marked as fixed but is not in 5.0.2: http://issuetracker.unity3d.com/iss...r-dot-speed-causes-animations-to-stop-playing

    It seems that when you play an animation (mechanim) with positive speed a counter goes up and up but if you play an animation with negative speed this counter goes down until it hits 0 and then the animation completely freezes until you get that counter above 0 again.
    I immediately hit this issue playing with mechanim for the first time which did not give me much confidence in it (along with the lack of zoom in animator editor!!).

    Can anyone confirm which version this will be fixed in or if it's some kind of desired behaviour (hope not)?
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
  3. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    2,277
    Thanks for answering, but how can you 'not' set a negative speed, does it throw a warning now if you try that?
    What is the easiest/best way to play an animation backwards in mecanim 5.1?
     
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Yes it does.

    Set your AnimatorState.speed to -1 and it should play backward, if you need to change the speed at runtime then you need to setup a controller's parameter. See link above for more documentation
     
  5. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    Hey Mecanim Dev!

    I've been waiting for Animator State Properties veeeery eagerly for proper animator rewind support for the users of my plugin (Chronos).

    I've just implemented them today and there seems to be a major bug with negative speeds. When you switch an ASP from positive to negative values, the normalized times become substracted by 1 and negative.

    This causes the animations to... kinda-almost-freeze? And sometimes stutter? It's a bit hard to describe, but it's the same issue that occured in pre 5.1 when you put Animator.speed to a negative.

    I logged it for debugging:


    I believe my setup is correct:


    The logging code is:

    Code (CSharp):
    1. for (int i = 0; i < animator.layerCount; i++)
    2. {
    3.    AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo(i);
    4.  
    5.    if (state.normalizedTime < 0)
    6.    {
    7.      Debug.LogWarningFormat("Speed Parameter: {0}\nNormalized Time: {1}", animator.GetFloat("TimeScale"), state.normalizedTime);
    8.    }
    9.    else
    10.    {
    11.      Debug.LogFormat("Speed Parameter: {0}\nNormalized Time: {1}", animator.GetFloat("TimeScale"), state.normalizedTime);
    12.    }
    13. }
    In Unity 5.0, when I used the speed property (which could be negative back then), I ran into a similar problem: the animations simply didn't work well at negative normalized times. So I had written a hacky fix that I ran at each update, playing the state back to the end. It used to work, but I figured it would become useless with ASPs. Now, not only is a hack still visibly needed, but it simply doesn't work anymore. I used to do something like this at each update:

    Code (CSharp):
    1. // Unlike Animations, Animators cannot play at negative normalized times.
    2. // If their current clip is at a time of less than zero, loop it from the end.
    3. protected virtual void FixAnimators()
    4. {
    5.    if (animator != null)
    6.    {
    7.      for (int i = 0; i < animator.layerCount; i++)
    8.      {
    9.        AnimatorStateInfo state = animator.GetCurrentAnimatorStateInfo(i);
    10.  
    11.        if (state.normalizedTime < 0)
    12.        {
    13.          // Set the playback position to the end of a loop (1) for that layer/state
    14.          animator.Play(state.fullPathHash, i, 1);
    15.        }
    16.      }
    17.    }
    18. }
    But you're saying it should work out of the box now. Or are these the issues you were saying would be fixed after RC1? Am I doing something wrong, or should I report this as an official bug? I'm asking here because you seem to be quite active on the forums, which I really appreciate :)

    Thanks!
     
    Last edited: Jun 13, 2015
  6. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Yes those issue are all fixed in 5.1.0.p1 which is already available to download
    https://unity3d.com/unity/qa/patch-releases

    Let me know if you find anything not working, all those issue should be fixed now
     
    LazloBonin likes this.
  7. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    813
    Works perfectly, no hacky fix needed. Thanks for the update!

    By the way, Unity 5.1 seems like quite a big release for the Mecanim team even though it wasn't boasted that much, so congratulations on all that good work!
     
  8. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Yes indeed, We did work on all the small annoyance that you guys are facing everyday.

    Thanks
     
    LazloBonin likes this.