Menu
CoddyTech

One Axis

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

Left and right are the two ends of one line, an axis. Input.get_axis() checks both actions and returns one number:

  • -1 while the first action is held (left)
  • 1 while the second is held (right)
  • 0 when neither is held, or both

Multiply that number by the speed, and the hero moves the right way with no if:

var speed = 300

func _process(delta):
	var direction = Input.get_axis("move_left", "move_right")
	position.x += direction * speed * delta

When direction is -1, x gets smaller and the hero moves left. When it is 0, the hero stays still.

challenge icon

Challenge

Easy

Walk the hero left and right at speed (300 pixels per second) with Input.get_axis(). Holding both keys keeps it still.

Try it yourself

extends Node2D

var speed = 300

func _process(delta):
	pass
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Input and Movement

Practice on your own: Godot playground