I've been working on a Threejs project where I have set up keydown controls for my player character in the animate function. Pressing 'a' and 'd' moves the character left and right, while 'w' and 's' move them up and down.
One issue I've encountered is that when I hold down one of the keys, the character's movement distance increases over time. This causes the character to jump farther after releasing and pressing the key again, eventually resulting in them jumping across the entire screen with just one press.
Below is a snippet of the code:
$(document).keydown(function(e){
if (e.keyCode == 87) {
player.position.y += 0.05;
}
if (e.keyCode == 83) {
player.position.y -= 0.05;
}
if (e.keyCode == 65) {
player.position.x -= 0.05;
}
if (e.keyCode == 68) {
player.position.x += 0.05;
}
});
I would really appreciate some assistance with this. Thank you! :)