How can I sense when i am not on the ground in an RPG?

Godot Version

4.2.2

Question

How can a character node detect when it is no longer on the ground (tile set) in a top down RPG?

It is 2d of course.

you can use the built-in func is_on_floor()

@pham150603 This will work in a sidescrolling game (and when using either move_and_collide or move_and_slide of CharacterBody2D), but not a topdown one!

@thepiglin83 If you have access to both the TileMap and the Player node, you can lookup the tile at the position of the player and then check yourself if it’s the right tile or not:

const LAYER = 0 # or whatever layer in the TileSet the ground tile is on
const GROUND_TILE = 0 # or whatever id the ground tile has on the layer

var source_id = $TileMap.get_cell_source_id(LAYER, $TileMap.local_to_map($Player.global_position))
if source_id == GROUND_TILE:
    # standing on the floor
else:
    # NOT standing on the floor

That does not work for a top down rpg.

So what would be the source Id of the tiles?

well that didn’t work and I tried on body exited and that didn’t work either.

This doesn’t require you to connect body_exited (or anything else). It simply uses the player’s position to look up the tile underneath it. The correct IDs for LAYER and GROUND_TILE depend on the TileSet you’re using, but should be trivial to figure out (since you can simply place the player on top of a ground tile, and run the function once and print the ID that gets returned). “that didn’t work” isn’t exactly enough information for me to help you any further.

1 Like

it says it cannot get global position on a null instance.

sorry I didn’t read it properly

wait i think I fixed it . I forgot to make it self.global position

Wait no it is saying the same thing still.

Now it works but it sometimes says I am not on ground when I actually am.

It stops working when I move out of a certain area.

I can’t debug a project I don’t have access to based on a vague, one sentence description. Either find the problem yourself or provide more context!

1 Like

I know it sounds vague but that is what is happening. I know for 100% sure that the tile map’s ID is 1 and there is a small area of the map that correctly prints the id the character is standing on. Every other place I stand on it prints -1

If get_cell_source_id returns -1, then the TileMap has no tile at that location – or at least no tile from the LAYER you provided.

Well that’s weird. I only have one tilemap on one layer And I know there is a tile there.