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 * deltaChallenge
EasyLet the hero walk in all four directions at speed (200 pixels per second) with Input.get_vector().
Try it yourself
extends Node2D
var speed = 200
func _process(delta):
pass
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground