- Home /
How do I determine which frame an animation is currently playing?
Hello gents.
I've finished the rough workings of my movement system and am assigning sound effects to various actions now just to add a bit more punch to my play tests.
I'm trying to determine which frame of my walk animation has just played so that I can use it as reference to fire a footstep sound effect. My animation has two frames I want to use as my points of "impact", but I don't know how to automatically fire a sound effect when those frames come to pass.
I've checked the manual repeatedly (in fact, it's the first place I go before I bother creating a new question), but unfortunately the script reference isn't showing me any practical applications for achieving this.
Could someone perhaps demonstrate to me in a short example code snippet (C# if possible) how something like this might work so that I can absorb the knowledge and get to writing it into my own script? :)
Thank you.
Answer by Jaap Kreijkamp · Jan 01, 2011 at 02:18 AM
Use AnimationState.time
for this, so you would get something like this: animation["walk"].time
and test if it's past a certain amount in time.
I read about that, but one thing I'm not understanding is what the time is based on. Is it just straight seconds based on the frame rate at which I originally key framed my animation, or a 0.0 to 1.0 range type thing? Just to be clear: If I have a walk cycle that is, say, 100 frames, and it was keyframed at 30 frames per second, does that mean it's roughly 3.3 seconds long and I should select a time within that range? Sorry to be long-winded. :)
Yeah, that's basically it, just analyse your animation and see at what moments in time the feet hit the floor, and then use those times to start the sound when time value passes those moments.