How do i detect collision with a specific tile in TileMap?

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

So i created a Tilemap with two different Tilesets.
One is the “floor” Tileset and the other is the “danger” Tileset.
Now i want to detect when my Player (KinematicBody2D) collides with the “danger” Tileset so he will loose 1 life f.e.
And later on i want to add new Tilesets “jump” to make him jump higher and so on…

I managed to do it with a new node (Area2D → Sprite (added the “danger” tile .png) → CollisionShape2D ) and added a script to the Area2D to detect collision.
But this process seems way to complex and when i want to add new tilesets in the future this would take way to much space.

hope i made my question clear :slight_smile: i really appreciate any answers
thank you in advance

:bust_in_silhouette: Reply From: Omar13

¿Do you necessary need to put your “danger” tileset inside that TileMap?
I think the better approach is:

(TileMap)TileWorld → Tilset1, Tileset2… TilesetN

(TileMap)TileDanger → Tileset1, Tileset2… TilesetN

Add this last tilemap into a “danger” group

Then add the damage logic inside the player

func damage() -> void:
    for i in get_slide_count():
        var collision = get_slide_collision(i)
        if collision.collider.is_in_group("danger"):

check in documentacion get_slide_count() and get_slide_collision() so you can understand what is doing (and maybe improve the code)
“die()” is whatever func you want to implement

whit this you can instanciate this scenes in any other scene with your player

Thanks a lot, so i made my tiles in a new Scene and everything works fine. One problem occured, maybe you can help me with it.
I added this to my player code

for i in get_slide_count():
	var collision = get_slide_collision(i)
	var danger = get_parent().get_node("Danger")
	if collision.collider == danger:

My function now looks like this.
I have 2 scenes, World1 and World2 where i instanced the Danger tiles, the World tiles and my Player.

how do i write the last line:


so that it changes to the root node of the current scene.
So when i collide in World1 with my Danger tiles my World1 scene reloads and
when i collide in World2 with my Danger tiles my World2 scene reloads.

thank you very much for your answer

sprengerjan | 2020-05-02 14:43

make an export var (export var allow you to change that var from editor)

export var next_level := ""


so in the world1 you can modify your player on editor (editable children) and put res://World2.tscn on the editor
on world2 same thing but res://World1.tscn

Omar13 | 2020-05-02 15:35

thank you very much Omar, that really helped a lot and gave me a tool to work with other things that bothered me :slight_smile:
grazie mille

sprengerjan | 2020-05-02 18:17