Controlling an animation's speed through playAction()

This code will play an animation:

obj.playAction("animation", 1, 100)

and this code will double its playback speed:

obj.playAction("animation", 1, 100, speed = 2.0)

But is there a way to change that animation’s speed midway though playback? For instance, varying the speed of walk cycle depending on that character’s velocity?

One problem I encountered is that calling this code every frame to pass in “speed = velocity” will reset the action to frame 1.

You could get the current frame, and play from there at the new speed (getCurrentActionFrame I think, check the API)

Okay, I got it to work using that method.

Its not ideal: if I call it every tick, it still starts at frame 0. Instead of triggering it every tick I call it ever 3 ticks.

Here’s an example. I couldn’t get the loop mode to work, so press spacebar play the animation again. Up and down arrow keys adjust speed.

Attachments

animation_speed.blend (493 KB)

If you need to trigger the code all the time it might be worth to switch to property mode of the action actuator. With the property you get full control over the animation. Just don’t forget you need to activate the ActionActuator everytime the property changes.