Menu
CoddyTech

Four Directions

Part of the Input and Movement section of Coddy's Godot journey. Lesson 4 of 28.

This game is seen from above, so the hero can also walk up and down. Input.get_vector() checks all four actions and returns a Vector2:

var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")

The order is left, right, up, down. Holding up gives Vector2(0, -1), because up is less y. Holding nothing gives Vector2(0, 0).

Multiply the vector by a speed and add it to position. This moves x and y at once:

position += direction * speed * delta
challenge icon

Challenge

Easy

Let the hero walk in all four directions.

  1. In _process(), store Input.get_vector("move_left", "move_right", "move_up", "move_down") in var direction.
  2. Move position by direction * speed per second.

Press Play: the arrows walk the hero up, down, left, right and on the diagonals. Then Check.

Try it yourself

extends Node2D

var speed = 200

func _process(delta):
	pass

All lessons in Input and Movement

Practice on your own: Godot playground