Menu
CoddyTech

Hero: Reach the Ledge

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

The goal is the high ledge with the flag. The hero carries a Banner, a hidden Label that says You made it!

On the ground the hero's position.y is 278. On the step it is 218, and on the ledge 158. So the hero is on the ledge when it is on the floor and position.y is below 170:

if is_on_floor() and position.y < 170:
	$Banner.visible = true

The is_on_floor() part matters. A jump from the ground also passes 170 for a moment, but in the air.

To win, jump the pit, then onto the step, then onto the ledge.

challenge icon

Challenge

Medium

Show the hero's Banner when it stands on the ledge, on the floor with position.y below 170. Then reach the ledge yourself.

Try it yourself

extends CharacterBody2D

var speed = 220
var acceleration = 1200
var friction = 2400
var gravity = 1000
var jump_velocity = -500
var start_position = Vector2()

func _ready():
	start_position = position

func _physics_process(delta):
	velocity.y += gravity * delta
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity
	var direction = Input.get_axis("move_left", "move_right")
	if direction == 0:
		velocity.x = move_toward(velocity.x, 0, friction * delta)
	else:
		velocity.x = move_toward(velocity.x, direction * speed, acceleration * delta)
	if direction > 0:
		$Look.scale.x = 1
	if direction < 0:
		$Look.scale.x = -1
	move_and_slide()
	if position.y > 400:
		position = start_position
		velocity = Vector2(0, 0)
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