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.

  1. In _physics_process(), above move_and_slide(), add gravity * delta to velocity.y.
  2. Use +=, so the fall gets faster every frame.

Press Play: the hero falls faster and faster and lands on the floor. Then Check.

Try it yourself

extends CharacterBody2D

var gravity = 1000

func _physics_process(delta):
	move_and_slide()

All lessons in Input and Movement

Practice on your own: Godot playground