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
EasyLet the hero walk up and down too.
- Under the
velocity.xline, setvelocity.ythe same way, fromInput.get_axis("move_up", "move_down"). - Use the same
speed.
Press Play: the arrows walk the hero in every direction. Then Check.
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()
All lessons in Input and Movement
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground