Detect if a RigidBody2D is standing parallel to the floor

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

My character is a box shaped, rigid body 2D set on rigid mode. I want it to only be able to jump when one of its edges is in full contact with the floor. Something like this:

enter image description here

I thought of checking the angle of the player, but that wouldn’t help if i’m standing over something like a ramp.

:bust_in_silhouette: Reply From: Elkondo

How about a bunch of really short RayCast2Ds from the player’s feet, and allow him to jump only if all of them return collision?

The problem with raycasts is you’ll have to add exceptions for everything that’s not floor or just use separate collision layer only for them and the floor. I think it’s the simplest solution though.

:bust_in_silhouette: Reply From: SIsilicon

Take your transformation’s y-axis:

var yaxis = global_transform.basis.y

Then compare the axis with your floor’s normal with a dot product.

var can_jump = -dot(yaxis, normal) > 0.95

You do know how to get the floor’s normal right?

SIsilicon | 2018-05-31 04:58

The basis doesn’t seem to work for Transform2D…

SqrtTwo | 2018-06-01 01:51

My bad. I was thinking of the 3D counterpart. Change that line to this.

var yaxis = global_transform.y

SIsilicon | 2018-06-01 03:46

I already have the normal vector of the floor, it’s solved now, thank you :D!

SqrtTwo | 2018-06-01 22:52

Your welcome. And if you want to…

Vote n’ Select :wink:

SIsilicon | 2018-06-01 23:30