Menu
CoddyTech

Hero: Run

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

Over the next lessons you build a platformer hero, one step at a time. Each lesson adds to the script from the lesson before. The level has ground, a pit, a step and a high ledge with a flag.

The hero is a CharacterBody2D with a CollisionShape2D and a Look node for its drawing. The ground, the step and the ledge are StaticBody2D nodes.

Start with gravity and running:

velocity.y += gravity * delta
velocity.x = Input.get_axis("move_left", "move_right") * speed
move_and_slide()

Run right into the pit and the hero falls out of the level. Later lessons fix that.

challenge icon

Challenge

Easy

Start the platformer: make the hero fall and run.

  1. In _physics_process(), add gravity * delta to velocity.y.
  2. Set velocity.x to Input.get_axis("move_left", "move_right") times speed.
  3. Call move_and_slide().

Press Play and run right: the hero runs off the ground into the pit. Then Check.

Try it yourself

extends CharacterBody2D

var speed = 220
var gravity = 1000

func _physics_process(delta):
	pass

All lessons in Input and Movement

Practice on your own: Godot playground