Walk Everywhere
Part of the Input and Movement section of Coddy's Godot journey. Lesson 10 of 28.
velocity has two numbers, and you can set each one. velocity.x is left and right. velocity.y is up and down. Each gets its own axis:
velocity.x = Input.get_axis("move_left", "move_right") * speed
velocity.y = Input.get_axis("move_up", "move_down") * speed
move_and_slide()For velocity.y, up comes first, so holding up gives -1. That moves the hero up the screen.
Hold up and right, and the hero moves both ways at once. Walk into the pillar at an angle to see it slide.
Challenge
EasyThe hero walks left and right. Let it walk up and down too, at the same speed, by setting velocity.y.
Try it yourself
extends CharacterBody2D
var speed = 200
func _physics_process(delta):
velocity.x = Input.get_axis("move_left", "move_right") * speed
move_and_slide()
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