Welcome!

You are just one step away from creating awesome games!

Sign up with Google

You need a Google account to use Defold

- 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 
end

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
    go.set_position(pos) 
end
  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.