What is the best way to add a sprinting mechanic to a game? More specifically, you would hold down a key, for example “Left Shift”, and for the time it’s being pressed, any other movement using WASD would have increased speed; and therefore when WASD is pressed without shift, movement speed is reduced back to normal.
Also please note, I am quite new to coding in Lua, and in the Defold engine in general, so any help is greatly appreciated.
Yeah well I tried using speed increase, but i couldn’t figure out how to make it revert back to normal speed, perhaps you could provide an example if you have the time?
local normalSpeed = 400
local runSpeed = 800
function init(self)
self.speed = normalSpeed
end
function on_input(self, action_id, action)
if action_id == hash("run") then
if action.pressed then
self.speed = runSpeed
elseif action.released then
self.speed = normalSpeed
end
end
end