Animation to the beat / restarting an animation

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:

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.

Simply use Animator.Play(0,-1, syncTime);

this will change the time of the current state playing

1 Like

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:

annie.Play(stateHashes[currentDance], 0, BeatCounter.getTime());

Seems to work nicely. Thank you.