Playing An Animation

This tutorial covers animation control through pure Python scripting.

Setup

  1. Create an Armature with animation actions

  2. Add an Always sensor + Python Controller

Animation Control Script

import bge

def handle_animation(owner):
    keyboard = bge.logic.keyboard
    anim = owner.playAction

    # Toggle animation with P key
    if keyboard.events[bge.events.PKEY] == bge.logic.KX_INPUT_ACTIVE:
        if owner.getActionFrame() == 0:
            anim("Walk", 1, 30, speed=1.0, blendin=5)
        else:
            anim("Walk", 0, 0, speed=0)  # Pause

    # Speed control
    if keyboard.events[bge.events.PLUSKEY] == bge.logic.KX_INPUT_ACTIVE:
        owner.actionSpeed = 2.0
    if keyboard.events[bge.events.MINUSKEY] == bge.logic.KX_INPUT_ACTIVE:
        owner.actionSpeed = 0.5

controller = bge.logic.getCurrentController()
owner = controller.owner
handle_animation(owner)

Key Features

  • Play/pause toggle with single key

  • Dynamic speed adjustment

  • Smooth blending between states