Hi!
Calling Input.is_action_pressed
for all four directions seems very redundant and like something very vital for a lot of games. That’s why I was wondering if there is a better way.
While checking the docs for the Input
class, I found the function Input.get_vector
which takes all four direction actions at once and returns a normalized Vector2
. It simplifies the code a lot and increases readability. The docs also mention WASD as a use case.
Here is my code:
func _physics_process(delta):
var input_vector = Input.get_vector("move_left", "move_right", "move_up", "move_down")
velocity = input_vector * 300
move_and_slide()
print("velocity = " + str(velocity))
What do you think?