Sprinting Mechanic for a sprite (SOLVED)

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. :slight_smile:

This depends on how you are moving around in the first place… you could increase your max speed value while holding shift for example.

How are you moving your character currently?

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?

Thanks for your response :slight_smile:

1 Like

Try something like this:

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
4 Likes

Thank you so much, Ross! I just had to make a few adjustments in my own code using your suggestions. Everything works perfectly now. ^^

4 Likes