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);
}
2
Answers
You can add a set timeout for your boost along with a boolean flag and make sure the user has some interval within each boost.
just add a variable named Pressed (or something dosent matter) set it to false and when the user uses that boost set it to true
and let a setTimeout with the cooldown u want that boost to be run in bg
when the time reaches 0 set Preseed to false
also make sure to check if the Pressed is false before running the boost code