- Select manual -

Moving game object

This example shows how to move a game object.

simple move simple_move.script

function init(self)
    self.center = vmath.vector3(360, 360, 0) 
    self.radius = 160 
    self.speed = 2 
    self.t = 0 

function update(self, dt)
    self.t = self.t + dt 
    local dx = math.sin(self.t * self.speed) * self.radius 
    local dy = math.cos(self.t * self.speed) * self.radius
    local pos = vmath.vector3() 
    pos.x = self.center.x + dx 
    pos.y = self.center.y + dy
  1. Store the center of rotation in the script instance (available through self).
  2. Store the movement radius.
  3. Store the movement speed.
  4. Store the elapsed time, in seconds.
  5. Increase the elapsed time with dt, the delta time elapsed since last call to update().
  6. Compute offsets along the X and Y axis. We’re using sinus and cosinus of the current time, scaled with self.speed, which will plot points along a circle with radius self.radius.
  7. Create a new vector3 which will contain the computed position.
  8. Set the x and y components of the vector to the rotation center plus offsets along X and Y axis.
  9. Set the computed position 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.