In my game, there is a “boost” option that pushs the player forward when they press Left Shift.
The problem is you can spam the button and go flying across the map.
To resolve this issue I want to add a small cooldown that will give the player good speed, but also won’t let the player exploit the feature by speeding across the entire map in 5 seconds.
I have tried looking up how to fix the issue but nothing helped because the code was in something like c++ or python.
Here is the code for my movement:
function Input() {
document.addEventListener('keydown', (event) => {
var name = event.key;
var code = event.code;
if (code == "ArrowRight" || code == "KeyD") {
velX+= 5
}
if (code == "ArrowDown" || code == "KeyS") {
velY+= 5
}
if (code == "ArrowUp" || code == "KeyW") {
velY-= 5
}
if (code == "ArrowLeft" || code == "KeyA") {
velX-= 5
}
if (code == "ShiftLeft") {
velX *= 2
velY *= 2
}
}, false);
}