I got a code which makes moving a crosshair. But her movement is not smooth. How do I solve?
local rot
function init(self)
msg.post(".", "acquire_input_focus")
go.set_position(vmath.vector3(0,-190,1))
rot = go.get(".", "euler.z") -- Get rotation
end
function update(self, dt)
go.set(".", "euler.z", rot) -- Set rotation
end
function on_input(self, action_id, action)
if action_id == hash("esq") and (action.pressed or action.repeated) and (rot <= 180) then
-- move left
rot = rot + 1
elseif action_id == hash("dir") and (action.pressed or action.repeated) and (rot >= 0) then
rot = rot - 1
end
end
You’re only moving the crosshair when the button is pressed and when it’s repeated. You should do it continuously instead. Also, module-local variables (rot
) are likely to break if you have multiple instances of the script.
function init(self)
msg.post(".", "acquire_input_focus")
go.set_position(vmath.vector3(0,-190,1))
self.rotation = go.get(".", "euler.z")
self.rotation_velocity = 0
end
function update(self, dt)
local rotation = self.rotation + self.rotation_velocity * dt
rotation = math.max(0, math.min(180, rotation))
self.rotation = rotation
go.set(".", "euler.z", rotation)
end
function on_input(self, action_id, action)
if action_id == hash("esq") then
self.rotation_velocity = action.value * 60.0 -- Adjust this factor to your liking
elseif action_id == hash("dir") then
self.rotation_velocity = -action.value * 60.0 -- Adjust this factor to your liking
end
end
6 Likes
Thank you! Worked 100%, now I’m going to study these functions.
1 Like