Best practice for ground detection?

Godot Version



just wondering, what’s the ideal way to check if a capsule collider is touching the ground. in unity, i would just do a spherecast, slightly below the bottom of the capsule. a normal raycast is too thin, so it only detects ground at one point. i used to just use a second collider- another sphere right below the capsule- but that caused problems in terms of like, interfering with the base capsule’s actual collision and physics.

it seems like there’s no spherecast in godot, otherwise that’s what i’d prefer to use. is there a way i can just access the actual collision info from the base capsule itself- and check if it is on the bottom? or should i just use a second collision shape (and how would i go about making sure that collider doesn’t conflict with the main one)?

The ideal way is using a raycast, like you would do in any game.

You could also do that.
You might want to use an Area instead of a collision to avoid that.
And properly set the layer/mask of the ground/detectors. There should no problems with one interfearing with the other if done properly.

Take a look at how collision layers and masks works.
But basically:
Layer = what the collider IS
Mask = what it collides/detects WITH

There is a ShapeCast in Godot. In which you can use a sphere as the shape.
But shapecast is more costly than raycasts, so you should avoid it in cases where you are testing for it every frame.

This depends on what you are using. CharacterBody3D works differently than RigidBody3D.
With RigidBody3D you can just use the _on_body_entered signal and you get the collided body passed as a parameter
With CharacterBody3D you need to use get_slide_collision(i) inside the process loop which is a bit more ugly.

i don’t want to use a linear raycast because i need to detect if any point on the bottom of the capsule has collided with something. i’m using a rigidbody3D. does _on_body_entered give me enough information that i could figure out where the collision happened? because what i want is to know if the bottom of the capsule touched something

Use an Area3d with a capsule collision shape.

You could check if the speed is going down, but that would could problems if hitting the side faces. The way to get the precise collision point an collision normal is currently horrible. You need to access it inside the _integrate_forces and iterate all collisions. SO I don’t recommend it.

Then I’d suggest you make a small sphere Area3D near the feet.
Just check if the velocity is going down when it collides as a sanity check.

1 Like