Help getting animated sprite to work with WASD keys

Godot Version

4.2.2

Question

I have a 2D top down game going with a sprite animation on the player. It works fine with arrow keys. I added WASD keys to the input map and the player sprite will move now when those keys are pressed but it doesn’t turn or play the animation. Here’s how input map settings look:

And here is the code I’m using:

func get_input():
if Input.is_action_pressed(“ui_right”):
play(“walk_rt”)
elif Input.is_action_pressed(“ui_left”):
play(“walk_l”)
elif Input.is_action_pressed(“ui_up”):
play(“walk_u”)
elif Input.is_action_pressed(“ui_down”):
play(“walk_d”)
else:
stop()

Thanks for any suggestions.

Two problems:

  1. From what I know, ui_[direction] is prebuilt into Godot. It is just the arrow keys. You will need to specify “right” instead of “ui_right” (repeat for every direction).
  2. Unless you have code that runs get_input() when the script detects user input, the function will not run. You have to use func _input(event: InputEvent).

These two changes should get your code working.

Thanks! That fixed it. My code now looks like this:

func get_input():
if Input.is_action_pressed(“ui_right”) || Input.is_action_pressed(“right”):
play(“walk_rt”)
elif Input.is_action_pressed(“ui_left”) || Input.is_action_pressed(“left”):
play(“walk_l”)
elif Input.is_action_pressed(“ui_up”) || Input.is_action_pressed(“up”):
play(“walk_u”)
elif Input.is_action_pressed(“ui_down”) || Input.is_action_pressed(“down”):
play(“walk_d”)
else:
stop()