Detect which side of RectangleShape2d is collided ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ErkiElbrecht
:warning: Old Version Published before Godot 3 was released.

Hi,

I am building my first platform game in this engine and i am having problems creating the main controllable character. Right now i am testing if the player is colliding:

if is_colliding():

and setting the jump count to zero so the player can jump again:

jump = 0

but this also means that the player can jump off of ceilings. To solve this i need to test somehow which side of the RectangleShape2D the player is touching and i have yet to find a solution.

Thanks in advance !

:bust_in_silhouette: Reply From: kidscancode

You could do get_collider().get_pos() and if the y coordinate is greater than the player’s y, then the object is below. However, that won’t work well for you if you have vertical obstacles/walls, so you’ll also need to use get_collision_normal() to ensure it’s a horizontal surface.

A simpler way to do this is to add a RayCast2D pointing downward. Then you can do if raycast.is_colliding() to allow jumping.

Big thanks, i didnt even think about raycast. Thanks mate

ErkiElbrecht | 2017-06-11 20:08

:bust_in_silhouette: Reply From: Phil88n

add 4 segment collision shape for each side, its a bit slow… but its the most simple way to do it