How to keep an object floating at a certain height above floor

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pro

i want to keep an object floating in space above the ground at a certain height keeping floor in mind tried some codes but its irrespective to the floor. i want it so if floor goes down it also goes down and up to

:bust_in_silhouette: Reply From: jgodfrey

This sounds like a good use for a RayCast or RayCast2D (depending on if you’re game is 2D or 3D). Assuming 2D, I’d just add a RayCast2D node (aimed straight down) to the “floating” object.

Using the intersect_ray method, you can get loads of information about any detected collisions, including what object triggered the collision (useful to detect the “floor”) and the position of the intersection.

Using that info, you should be able to adjust the height of the “floating” object to be some distance above the point of collision.

tnks i definitely try it

pro | 2020-04-07 08:45

sorry to disturb could u give a clearer example its a 3d world im not exactly getting the raycast has described in the doc

pro | 2020-04-07 10:09

:bust_in_silhouette: Reply From: kidscancode

RayCast can work. Alternatively, you could extend your collision shape under the object, or add an additional one.

thanks i added a shape afterwards but ill try using raycast

pro | 2020-04-07 08:44