I’m working on click(touch)-to-move for my project and the script is not consistent. I found a script here on the forum and added a few things, like acceleration, but this issue was happening before that as well. Basically, sometimes when the GO reaches the point that was clicked it stops and sometimes it doesn’t. There are no error messages so I don’t know why it’s happening. It seems to be that when you click on the screen further away from the GO it’s more likely to not stop than when you click close to it.
The main collection only has 1 GO (player), and the GO has only 1 script(below) and a sprite. No collision objects of any kind, so there’s nothing to complicate things.
My question is, simply, does anyone know why this code would work sometimes but not other times? And a follow up question; if it’s not obvious, what debugging things can I do to try to solve it?
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()
self.direction = vmath.vector3()
self.moving = false
self.speed = 0
self.acceleration = 5
end
function update(self, dt)
if self.moving then
local position = go.get_position()
if vmath.length(self.direction) > 0 then
self.speed = self.speed + self.acceleration * dt
position = position + self.direction * self.speed * dt
go.set_position(position)
end
if target_reached(self.target_position, position) then
self.speed = 0
self.moving = false
self.target_position = vmath.vector3()
go.set_position(position)
end
end
end
function on_input(self, action_id, action)
if action_id == input_touch and action.released then
self.target_position = vmath.vector3(action.x, action.y, 1)
self.moving = true
self.direction = vmath.normalize(self.target_position - go.get_position())
end
end