Search Unity

Proper handling of animation events when setting normalised time

Discussion in 'Animation' started by lamorna, Oct 2, 2019.

  1. lamorna

    lamorna

    Joined:
    Oct 2, 2019
    Posts:
    9
    I’m looking to drive various animations by setting the normalized time parameter using a tween, like so:

    animator.SetFloat(“parameterName”, tweenTick);


    The animations have sound events which fire as expected as the animation progresses, but at the start of a new state I get bogus events fired, which I’m assuming is the transition making the animator think the playhead has been scrubbed back across the animation.

    I found setting the states' speed to 0 solves the problem locally, but transitioning from regular animations which are not driven by the normalised time still result in the bogus events

    Does anyone have any experience with this interplay of driving animations with normalised time, regular animation states and proper firing of animation events? Thank you.
     
    Last edited: Oct 5, 2019
    Emanx140 likes this.
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    That behaviour is part of the underlying Playables API. If you set the time of a playable, its next update will trigger any events between the previous and current time, but if you set the time twice then that doesn't happen. I don't know anything about the internals of the Animator Controller system, but I suspect that animator.SetFloat will not set the playable's time directly so you won't be able to solve it by just calling SetFloat twice (and there might not even be any simple solution since Animator Controllers are stupid and don't give you direct access to anything).

    But if you use Animancer (link in my signature) you won't have that problem and you get much better scripting control over everything without needing to mess around with Animator Controllers or parameters at all. Here's the documentation for controlling the time of an animation.
     
  3. lamorna

    lamorna

    Joined:
    Oct 2, 2019
    Posts:
    9
    Thank you, appreciate the quick response. I took this as unsolvable in a general sense, my bandaid solution was to check the time stamp of each animation event against the normalised time to determine validity. If I need a more robust solution I'll be sure to check out Animancer, it looks like a powerful tool.
     
  4. lamorna

    lamorna

    Joined:
    Oct 2, 2019
    Posts:
    9
    In addition, not only does driving the animations with the normalised time parameter produce bogus animation events, it appears to produce blending anomalies under certain conditions as well.
    I have a scenario with two parameter driven locomotion states separated by a regular animator driven idle state, where the transition to the idle is interruptible. I see blending from frames at the start of the first animation state if the idle transition is interrupted, which seems consistent with the normalised time parameter not actually updating the animator playhead internally.