How can I distinguish between different tile types? For example, I want to know if I'm coliding with grass, stone, ice, etc...
In a 2D game I want to check if the player is standing on metal, ice, etc.. I can’t make it
The player has a Raycast2D node, which checks the ground collision.
I think it’s possible to add a sliding effect by adding a physics layer to a tilemap although I’m not sure how I would do it. Anyhow here’s a video about another way you can get this result
I think the gist of it is adding a raycast that checks the name of the floor it is colliding with and then changing the player logic based on what that raycast is returning
What do you mean you don’t trust it? Another (somewhat unpractical) way would be to get some position below the player’s feet and check what tile is placed there:
const ICE_SOURCE_ID: int # set to id of atlas
const ICE_ATLAS_POSITION: Vector2i # set to the tiles position in atlas
@onready var tile_map_layer: TileMapLayer = $"../" # set to the path to the active TileMapLayer
func is_on_ice() -> bool:
var feet_position: Vector2 # set according to the position of the player
var tile_position: Vector2i = floor(feet_position / Vector2(tile_map_layer.tile_set.tile_size) - tile_map_layer.position)
var tile_source_id: int = tile_map_layer.get_cell_source_id(tile_position)
var tile_atlas_position: Vector2i = tile_map_layer.get_cell_atlas_coords(tile_position)
if tile_source_id != ICE_SOURCE_ID or tile_atlas_position != ICE_ATLAS_POSITION:
return false
return true
I really don’t recommend you do it purely in code, using the physics system seems much more intuitive and like this you can’t even set the name of the tile but have to check for it with id and atlas position.
EDIT: I think this approach is better if you want to dynamically check for many different tile types depending on user input or map settings because setting the collider’s name as the tutorial suggests isn’t a very safe option and can lead to overlap.