Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Godot: Detect if a RigidBody is on the ground!

To detect if a rigidbody is on the ground, you do the following:

Make a raycast that points down just below the rigidbody so it collides with the ground.

Put all ground nodes in a group called "ground".

Make a variable called "on_ground = 1"

In GDScript (Sudo code): If raycast is colliding, then if the collider is in group "ground", then on_ground = 1.

If jump key is pressed, then if on_ground == 1, then jump and on_ground += 1

Now, if your on the ground, on_ground always is set to 1. When you jump, on_ground has to be equal to 1, but you add 1 to on_ground which makes on_ground == 2, which enforces a single jump! And on_ground then stays at 2 when your in the air, and since it has to be 1 to jump again, you cant jump until your on the ground, which will then set on_ground back to 1, and you can jump again.

If you want a double jump or more, you simply do:

If jump key is pressed, then if on_ground < 2 or 3 or so on, then jump and on_ground += 1

Hope this wasn't too complicated, I tried my best!

Support this post

Did you like this post? Tell us

Leave a comment

Log in with your itch.io account to leave a comment.

I’m not sure if it’s RigidBody3D or 2D, but you shouldn’t use RayCast for ground detection. Because it’s a line, not a shape. There’s a chance the player get stuck on an edge, but on_ground won’t be 1. Raycasts are awesome, but this is flawed.

True. But it works for some, including myself.

(+1)

That’s great :D