How to check which tilemap my player collides with and emit a signal?

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

I’m making a simple 2D platformer.

I have two tilemaps. Tilemap Structures is for walls, ceiling and floor and anything else that the player can’t go through.

Tilemap Ramps is for slopes. I want the player to lose control of his character if the character is standing on a slope, so the player slides down with gravity force.

So far, to check if the player can move, I’m using is_on_floor() function which detects both tilemaps as floor.

How do I check which tilemap the player is standing on? Is there a different approach that I can use for the sliding mechanic?

:bust_in_silhouette: Reply From: kidscancode

When your kinematic character collides, the KinematicCollision2D returned by get_slide_collision() will tell you what you collided with.

Docs: KinematicBody2D: detecting collisions

Hey sorry for the late reply. I got stuck on teraria update x)

I tried using the code in the docs you linked, but ran into a problem. When my character was standing above both of my tilemaps, that is, between two tiles, it sometimes behaved as if I’m not standing on a floor and instead I’m already on a ramp. However, the collision box kept me standing on the floor and as such, I couldn’t move the character.

I’m pretty sure it could be fixed somehow, maybe by using a circle collider instead of a box collider.

In the end, I made three raycast2d nodes on my character (I posted the same question on reddit and got this idea from a user there), one originating from bottom left, second from bottom right and last one from bottom center of the players collision box.

I’m checking the tilemap using get_collider().name and if either of those raycasts collide with my Tilemap for Structures I know I’m on the ground.

Thank you anyways.

EdgyAvalanche | 2020-05-20 20:28