In Godot how do you determine if the feet is just on the ground?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Joe0239
:warning: Old Version Published before Godot 3 was released.

enter image description here

In other words how do I determine if the back off the feet is just on the 0 y axis?

:bust_in_silhouette: Reply From: avencherus

One method is to align your object in its own scene so its feet are touching 0 on the origin.

Then whenever accessing the object, its position is always where the bottom of the feet are located.

Another easy method is to use Position3D nodes, and place them where the Y is the bottom of the feet. Name is something obvious like feet_position, and call for its position in code whenever you need to query where the feet are at.

A third is using some offset value. It is the same as Position3D, but you find out the Y distance difference between the origin point of the root node and the feet. Then add or subtract this offset (depending the way you calculate it) from the node position.

Thank you very much :slight_smile:

Joe0239 | 2017-12-27 23:11

:bust_in_silhouette: Reply From: Joseph Catrambone

I have a 3D ray at the bottom of my player primitive. It’s a little hard to see in this picture because it’s perfectly aligned with the axis, but there’s a small RayCast arrow pointing down.

Player Hierarchy

In my code, I grab a reference to this raycast at startup and then keep checking to see if it’s intersecting with something. If it is, I know the object is on the ground. You can do this with the is_colliding() method. Try experimenting with the length of the vector and make sure it’s pointing in the right direction. Here’s the sample code:

# In init:
onready var down_ray = get_node("DownRay")

func _process(delta):
    if !down_ray.is_colliding(): # If we are not on the ground:
        acceleration.y = gravity # Start falling.

A minor advantage to this is you can change the length of the ray and its position, so you don’t have to have your object at the origin of the space. This might be considered a downside, but it’s simple to implement on code and it takes advantage of the editor’s visuals.

:bust_in_silhouette: Reply From: UnsignedFoo

Why not simply use a collider or getting the global position of the object??

Sometimes the raycast is useful to check if the character/player/object isn’t “flipped” and touching the ground. For example, if I use a car as the collider, it could show that the car is on the ground even when upside-down. Same for the player, it might consider the player on the ground even if it is colliding with a wall or something.

The global position is another way out, however it wouldn’t work too well if you have more “floors” on your level.

EDIT: Nevermind, I had misread it and thought that the OP wanted to check if the player was on the ground, but as he wants to know the axis of his feet, the Position3D solution works just fine.

Hozer | 2021-04-07 00:34