Search Unity

Animation to the beat / restarting an animation

Discussion in 'Animation' started by rrh, Jan 13, 2015.

  1. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    Suppose I have a dancer who can do a bunch of dancing animations and I want to switch between them but ensure it doesn't get out of sync with the music.

    I have some animation loops, that all sync nicely with the music if I just start them and let them loop. But once I start switching between loops with the Animation controller, it doesn't sync up as nicely.

    This post was getting at what I'm looking for but there's a gap in the answer:
    http://answers.unity3d.com/questions/649220/syncronise-animation-of-instantiated-objects.html
    The answer there is "Every time this goes up by a whole number, start the animations over." but okay, how do I start the animations over?


    I also created a BeatEvent that is invoked on the beat, so I would probably use that to start animations over if there's a way to do that.
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    rakkarage likes this.
  3. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
    Okay.
    I've played around with it.

    So now instead of mechanim transitions, I'm using .Play() to switch states.

    I have BeatCounter class with some static functions, one of which is not a .getTime() function which returns
    .GetCurrentAnimatorStateInfo(0).normalizedTime

    So when I switch to a new dance, I use that as the start time like so:
    Code (csharp):
    1.  
    2. annie.Play(stateHashes[currentDance], 0, BeatCounter.getTime());
    3.  
    Seems to work nicely. Thank you.