The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

enter image description here

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

in Engine by (194 points)
edited by

2 Answers

0 votes

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.

by (16 points)
0 votes

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

by (26 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.