I have created the player control like this:
w - go up
a -go left
s - go down
d - go right
And also player can move diagonally, if he will press constantly keys:
a+w, w+d, s+d, a+s.
But if player press a+w+d, he is move only up.
How can I add limit to only press two certain keys?
You could use a global variable with the number of currently pressed keys. Each time you press a key, it gets incremented by 1; each time you release a key, it gets decremented by 1.
On your conditions to check if a given key is pressed, add a second condition that keys pressed must be less than 2 for that key to do anything.
I’m unaware of a built-in solution.
- add a variable to store the number of keys pressed
- add events to increase/decrease said variable when the movements keys are pressed/released
- add conditions to apply the movements only when the variable < 2
- add 2 variables, one for vertical_movement, one for horizontal_movement
- add events to change the variables (eg: vertical_movement= “moving_up”)
- add conditions to apply the movements accordingly (eg: if vertical_movement≠ “moving_up” → move down)
Remember, put first the check (if movement≠…) then the action (set movement…)
But through my experiments I found a very easy solution:
If the combination of pressed keys is wrong (like w+s or w+a+d) I just not added action. So, if these wrong keys are pressed, the player is not moving.