Help getting height of 2D player so they can stick to walls without looking weird

Godot Version

4.4

Question

Hello! I’ve recently got back into Godot after not using it for over a year(!) so I’m a bit rusty with stuff I’m pretty sure isn’t too tricky to do.

In this case, I’m back to working on a 2D platformer where the player can stick to walls (and wall jump). The issue is, I don’t know/can’t remember the best way to check

a) if the player is high enough off the floor to warrant sticking to a wall (right now they can stick to any wall as long as they press jump first, even if they only ‘jump’ like 2 pixels up)

and

b) if the player’s torso is connecting with a wall (at the moment they can even stick to walls if even just a bit of their head hits it, which is funny but not useful)

All the code works for everything else, and the player uses a typical collision shape 2D that covers their main body, head and legs.

The line of code to stick is super simple, but I know it needs some extra conditional(s) to get the result I’m after:

func apply_gravity(delta):
if not is_on_floor():
velocity.y += gravity * movement_data.gravity_scale * delta
if is_on_wall_only():
velocity.y = 0 #this if clause lets player stick to walls

If anyone needs any more details/code, please let me know!

i think raycast2d’s should solve your problem. Just put one that points towards the ground and one that points to the side on the torso of the player.

An alternative are the built-in methods “is_on_floor()” and “is_on_wall()”