Menu
CoddyTech

Fall With Gravity

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

This game is seen from the side, and the hero starts in the air. Gravity makes it fall faster and faster, because it adds to the falling speed every frame.

Down is more y, so gravity adds to velocity.y:

var gravity = 1000

func _physics_process(delta):
	velocity.y += gravity * delta
	move_and_slide()

gravity is how much the falling speed grows in one second. One frame gets gravity * delta of it. velocity itself still never gets delta.

When the hero lands on the floor, move_and_slide() stops it and sets velocity.y to 0.

challenge icon

Challenge

Easy

Make the hero fall with gravity. Add gravity * delta to velocity.y every physics frame, before move_and_slide(). It should fall faster and faster and land on the floor.

Try it yourself

extends CharacterBody2D

var gravity = 1000

func _physics_process(delta):
	move_and_slide()
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