Menu
CoddyTech

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 icon

Challenge

Easy

Let the hero walk up and down too.

  1. Under the velocity.x line, set velocity.y the same way, from Input.get_axis("move_up", "move_down").
  2. 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

Practice on your own: Godot playground