- Select manual -

Spinner animation

This example shows how a game object is rotated in discrete steps, matching the graphics of the progress spinner.

spinner spinner.script

function init(self)
    self.t = 0 
    self.speed = 16 

function update(self, dt)
    self.t = self.t + dt 
    local step = math.floor(self.t * self.speed) 
    local angle = math.pi / 6 * step 
    local rot = vmath.quat_rotation_z(-angle) 
  1. Store a timer value (seconds elapsed) in the current script component (accessed through self).
  2. A speed value. How many rotation steps to perform each second.
  3. Increase timer value with the delta time elapsed since last update().
  4. Calculate which step to rotate to.
  5. Calculate rotation angle (in radians) based on which step to rotate to.
  6. Create a rotation quaternion with angle rotation around the Z axis.
  7. Set the rotation on the current game object.

If you want to play with these examples, you can get the project on Github.

Do you want to see more examples? Why not write a few yourself and submit a pull request? We love contributions.