Godot WASD Keyboard Setup

Godot WASD Keyboard Setup

A WASD input keyboard is a keyboard with the W, A, S, and D keys that are used for directional input in video games. These keys are often used in first-person shooters, third-person action games, and role-playing games (RPGs).

WASD Keyboard Setup

1. Setup the Input Actions

  • Open the Project Settings
  • Go to the Input Map tab
  • Create 4 new actions called "move_up", "move_left", "move_down", and "move_right"
  • Clicking on the plus icon to the right you can add the W, A, S, and D keys by hitting the key on your keyboard

2. Script

Now you can check if the player has pressed the W, A, S, or D keys by changing to Input.get_axis("move_left", "move_right")

extends CharacterBody2D

@onready var animation_player : AnimationPlayer = $AnimationPlayer
@onready var sprite : Sprite2D = $Sprite2D

const SPEED = 300.0
const JUMP_VELOCITY = -400.0


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
		animation_player.play("jump")

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction := Input.get_axis("move_left", "move_right")
	if direction:
		velocity.x = direction * SPEED
		animation_player.play("walk")
		if velocity.x < 0:
			sprite.flip_h = true
		else:
			sprite.flip_h = false
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		animation_player.play("idle")

	move_and_slide()

3. Conclusion

This is a simple way to setup WASD keyboard controls in Godot. You can use this as a starting point and build on it from there.

Follow the development of my game Coin Grab on github:

https://github.com/Zero-Fall-Studios/coin-grab



Newsletter

Stay up to date with the latest articles, tutorials, and news.