How to detect tilemaps metadata in 2d platformers?

Godot Version



I have a CharacterBody2D that represents my player
And I have a Tilemap for the world map, in this tile map I have a ground tile with a metadata set to type = “ground”

How can I detect wich tile is my player on top of? I have tried world_to_map but it does not seem to work, neither the collision detection (wich returns the tilemap node, not the tile)

You need to get the TileData and use get_custom_data() if you data is in the custom data layer.

This is indeed used to get the Tile…data, but my initial problem is that I can’t get the exact tile that the player is, since he is on top of the tile using local_to_map does not make any sense I guess, it would if the game is a top down and the player is on top of the tile.

Sorry if I made it unclear :frowning:

You could try using a RayCast2D or ShapeCast2D to detect in which tile floor the player is.

1 Like

I see, I understand. I think you can still use local_to_map(), but you will need to change the y position, increasing it by an amount enough to be in the tile below position. For example:

local_to_map(player_position.x, player_position.y + modifier) # Convert to_local() if using global position

Now that you have these coordinates, you can use get_cell_tile_data() once you provide this coords and the layer as parameters:

This should return TileData, which you can finally use get_custom_data() or get_custom_data_by_layer_id() to get the custom data associated to that tile.

The way I do this is to use RayCast2D

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.