Keyboard Movement in Godot

How do you move an object in response to keyboard input in Godot? Read on and find out! This article is similar to “Unity3D: Moving an Object with Keyboard Input“, but for Godot. Also, I literally started learning Godot yesterday, so don’t expect any expert advice!

Setting the Scene

Start by creating a new project. Then, within the “Scene” tab on the left, click the “Other Node” button:

In the modal that comes up next, search for and select a “ColorRect”, then click the “Create” button:

With that, you’ve just added a white square to the top-left of our 2D window. You can optionally change the colour in the Inspector to the right, and/or drag the white square to a more central position:

When you’re happy, press the Play button near the top-right of the Godot editor, or press F5. You’ll be prompted to select the main scene; go with “Select Current”:

Then, you’ll be able to save the current scene (i.e. the lovely square). You can leave the defaults and simply press “Save”:

Once the scene is saved, the game starts running, and you can see what your square would look like in a game:

Creating a Script

In order to move the square when we press a key, we have to add a script. To do this, right-click on the ColorRect in the Scene tab on the left, and select “Attach Script…”:

In the next modal window, leave the defaults and click “Create”:

This opens the Godot script editor, and gives you a little code template written in GDScript to get you started:

Programming Keyboard Movement

We now have a square that we can see in a Godot window, with a script attached to it. We can now program it to do our bidding!

Near the top, just below the “extends ColorRect“, add the following:

var speed = 400

Then, in the “func _process(delta)“, replace “pass” with the following:

	if Input.is_action_pressed("ui_left"):
		position += Vector2.LEFT * delta * speed

Here we’re using the “Input.is_action_pressed()” function to determine whether a key was pressed, and the “ui_left” value specifies that we’re specifically checking whether the left arrow key was pressed. If that’s the case, then we update our square’s position. Here’s a breakdown of that second line:

  • Vector2.LEFT is literally something (a mathematical vector) pointing towards the left.
  • delta is a value that gets passed to the _process() function each time, indicating how much time passed since it was last called. Because framerates can vary, it helps us ensure smooth movement regardless of how the framerate changes.
  • speed is that variable we just defined earlier. We can change that as we like to adjust how quickly the square moves when we press the left arrow key.
  • position is our square’s position. It is itself a vector, and we update it by adding other vectors to it.

This is enough to make the square move to the left when you press the left arrow key, as you can quickly verify if you run it. In the same way, you can support the other arrow keys. Below is what the completed script should look like, also after removing the “_ready()” function which we don’t need here:

extends ColorRect

var speed = 400

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if Input.is_action_pressed("ui_left"):
		position += Vector2.LEFT * delta * speed
	if Input.is_action_pressed("ui_right"):
		position += Vector2.RIGHT * delta * speed
	if Input.is_action_pressed("ui_up"):
		position += Vector2.UP * delta * speed
	if Input.is_action_pressed("ui_down"):
		position += Vector2.DOWN * delta * speed

At this point, run it (F5 / Play button) again and use the arrow keys to move the square in any direction, including diagonally:

A Better Way

The 2D movement overview page in the official Godot documentation demonstrates an “Input.get_vector()” function that can obtain a single vector representing the combined movement resulting from any of the arrow keys, effectively replacing the need for separate conditional statements. Let’s try that:

extends ColorRect

var speed = 400

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	position += Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") * delta * speed

It works just as well, but it’s a lot more concise! Lovely.

Summary

We’ve seen how to implement basic keyboard movement using Godot. The ColorRect is a 2D element that’s very handy to start playing with. Once we attach a script to it, we can react to keyboard movements using the static Input class. We’ve seen how to do that using two different functions: is_action_pressed(), which tells us whether individual keys have been pressed, and get_vector(), which can check all the ones we want at the same time.

Leave a Reply

Your email address will not be published. Required fields are marked *