I couldn’t find an example for this anywhere so I had a go myself starting with the player script from the War Battles tutorial. Basically, the game object should move towards the touch or click position on input and stop when it reaches the target.
local input_click = hash('click')
local input_touch = hash('touch')
local function target_reached(target, current)
return math.floor(target.x) == math.floor(current.x) and math.floor(target.y) == math.floor(current.y)
end
function init(self)
msg.post('.', 'acquire_input_focus')
self.target_position = vmath.vector3(0, 0, 0)
self.direction = vmath.vector3(0, 0, 0)
self.moving = false
self.speed = 50
end
function update(self, dt)
if self.moving then
local position = go.get_position()
position = position + self.direction * self.speed * dt
go.set_position(position)
if target_reached(self.target_position, position) then
self.moving = false
self.target_position = vmath.vector3(0, 0, 0)
end
end
end
function on_input(self, action_id, action)
if action_id == input_click or action_id == input_touch then
self.target_position = vmath.vector3(action.x, action.y, 0)
end
if vmath.length(self.target_position) > 0 then
self.moving = true
self.direction = vmath.normalize(self.target_position - go.get_position())
end
end