Search Unity

Bug Manually updating animator incorrectly fires animation events!

Discussion in 'Animation' started by esco1979, Jun 29, 2020.

  1. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    138
    So I have run into an odd bug that I'm not quite sure how to fix. Whenever I change animations for the player character, I use a method that first calls the line anim.Crossfade("animName", 0, 0, 0) and then the next line say anim.Update(Time.time).

    Now, this seems to work fine at IMMEDIATELY updating the animator to the correct state, but I noticed that with any state that has an animation event in it, when that update line is called, it also immediately fires the animation event(s) of the old state as it goes to the new state. Even though the state is clearly NOT at the correct timestamp when the animation event should fire.

    I tried an experiment or two, and if I instead change Time.time to Time.deltaTime, the bug doesn't occur. But then my animations are off slightly and deltaTime is clearly incorrect here.

    Can anyone explain why this is how or how to fix it? I thought maybe it was a glitch because I was using an older version of Unity, but I updated to the newest version and it is still happening.

    EDIT: I found this old thread that is possibly related to this issue. Humourosly using anim.Update(0) seems to work fine but not anim.Update(Time.time): https://forum.unity.com/threads/animator-update-0-firing-events-in-unity-5-6.467897/
     
    Last edited: Jun 29, 2020
  2. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    138
    BUMP!!!!
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,338
    Why would you Update(Time.time)? The argument to Update is deltaTime - how many seconds you want to mvoe the animator forwards. That's going to be a large number if you use Time.time, so you're going to be looping through your animation very many times, which is what's going to cause the events to be fired.

    Check the docs.
     
  4. esco1979

    esco1979

    Joined:
    Mar 18, 2014
    Posts:
    138
    That isn't correct. DeltaTime skips a frame when used here. Whenever I used Time.time, the animation would start exactly where it should. :)