Get colliding body which makes is_on_floor()/is_on_wall()/is_on_ceiling() true

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

I have a KinematicBody2D player node for which I check is_on_floor(), is_on_wall() and is_on_ceiling() in each iteration of the physics loop and execute the appropriate code, which works well when colliding with genuine floors/walls/ceilings.

The problem is that the enemies (also KinematicBody2D nodes) cause these functions to return true when the player collides with them. I only wish for the code I’ve written to execute when the player collides with genuine floors/walls/ceilings (which I’ve identified by adding them to a dedicated collision layer).

Is there any way to easily get the collider which caused the engine to return true when is_on_floor was called (if there are multiple, just the first would be fine)? If not, is my only option to put raycasts and do a manual check, effectively eschewing the is_on_thing approach?

:bust_in_silhouette: Reply From: MrEliptik

If you use move_and_collide the result is a KinematicCollision (https://docs.godotengine.org/fr/stable/classes/class_kinematiccollision.html#class-KinematicCollision) and allows you to check the collider.

If you use move_and_slide() or move_and_slide_with_snap() though, you can’t access the collider. If would suggest to add an Area2D with a slightly larger collision shape. With the Area2D_body_entered signal you can check the body that’s colliding with you.

:bust_in_silhouette: Reply From: ClumsyDog

Maybe get_slide_collision is what you want: https://docs.godotengine.org/en/stable/classes/class_kinematicbody2d.html#class-kinematicbody2d-method-get-slide-collision

Thank you, I sought precisely this.

Godont | 2021-03-30 20:37