- Home /
How to play the same animation multiple times?
I've run into a problem I can't seem to resolve nicely.
I have a character which can react to "slaps" by playing back an additive get-hit animation. However he can be slapped quite frequently and I want each slap to get a slap reaction. But sending a crossfade call to the same animationstate just makes the current animation keep playing.
What I'm looking for is a way to make an animation blend to itself. In other words if "slap_left" animation is playing, and is called once again, the first playing version should blend out, while the same animation blends back in at time 0.0.
Setting time or rewinding it is too snappy, and the other solution I'm considering now is to have duplicates of the same clip and call them slap_left1 and slap_left2, but that seems a bit hackish.
Answer by Waz · Jul 13, 2011 at 11:32 AM
I believe what you want is CrossFadeQueued with PlayNow mode:
animation.CrossFadeQueued("slap_left", 0.2, QueueMode.PlayNow);
Yes, that sounds contradictory, I guess they should have called it "CrossFadeDuplicate".
Beautiful, that's exactly what I needed. And yes, a bit misleading. I never thought to check out the Queuecommands when I wanted an animation to play immediately as a duplicate, ins$$anonymous$$d of playing after (as if queued) :)
There's only one problem that I noticed, when I use CrossFadeQueued, it refuses to play additively. With normal CrossFade it plays back additively, with CrossFadeQueued it plays back as a normal fullbodied animation. Is that a limitation on the queued playback, or simply a bug?
Note that, since it's a duplicate, you'll have to set the blend mode on it:
var newstate = animation.CrossFadeQueued(...);
newstate.blend$$anonymous$$ode = AnimationBlend$$anonymous$$ode.Additive;
Thanks again! I didn't realize it was a duplicate animationstate until after I posted the question, noticed the little ": AnimationState" after the CrossFadeQueued command in the Scripting Reference. I ended up writing it like this:
animation.CrossFadeQueued("push_right",0.1f,Queue$$anonymous$$ode.PlayNow).blend$$anonymous$$ode = AnimationBlend$$anonymous$$ode.Additive;
Thanks a gazillion for the help, smooth way of playing impact animations achieved! This thing had me scratching my head for way too long.