Detect if one of player's foot is on ledge?

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

So I’m making a platformer game that uses tiles from a tilemap, and I was wanting to add a function where if one of my character’s feet is standing on the edge of the platform, but the other is hanging off and not touching anything, my player would change into a different sprite. (Kind of like from Spelunky, where you stand on an edge and lose your balance.) So it seemed to me like a pretty simple task to overcome, but after testing around with raycasts, area2ds, and more, it simply just won’t detect the ledge. Either I’m not using these tools properly, or there’s a different way. Any help is appreciated.

You need to include more detail, as you’re probably just missing a setting or something.

The way I’d go about it is simple, just have two raycasts, one for each side of the player hitbox’s bottom. If they’re both intersecting an object then the player is on solid ground, if only one of them is then the player is not.

Check the collision layers and collision masks to make sure they’re set up correctly. There’s not much people can do to help you without any info, i.e. your code.

denxi | 2020-04-30 15:24

I finally figured it out after a day’s, work. I put two area2ds on either bottom side of the player that are halfway in the ground, then did a similar thing like @denxi with the raycasts, but, instead with the area2ds. But instead of doing it on the tilemap, I worked the code on a staticbody2d block with the same sprite as the tilemap’s, and it seemed to affect the whole tilemap even though the code was solely on the block’s script, I’m not completely sure why, but it’s a blessing to not have to replace the tilemap with tons of separate blocks.

user_name | 2020-04-30 16:24