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
EasyStart the platformer: make the hero fall and run.
- In
_physics_process(), addgravity * deltatovelocity.y. - Set
velocity.xtoInput.get_axis("move_left", "move_right")timesspeed. - 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
5Project: Platformer Hero
Hero: RunHero: JumpPractice on your own: Godot playground