How can i recognize things over my head?

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

I’m making a platformer and I want the character to duck, I can do it with different collision shapes, but I want to recognize when there’s something on top of me so i don get up and glitch. if is_on_floor(): if Input.is_action_pressed("ui_down"): $AnimatedSprite.play("Duck") $Normal.set_disabled(1) $Duck.set_disabled(0) elif $DuckDetection.(I_dont't know what to put here)(): $Normal.set_disabled(1) $Duck.set_disabled(0) else: $Normal.set_disabled(0) $Duck.set_disabled(1)
“Normal” is my standing collision shape, “Duck” is my ducking Collision shape and “DuckDetection” is the area i want to use to detect the ceiling.
I’m new to Godot so please explain.

Can you please format multilined code like this instead? It’s much easier to read.

if is_on_floor():
    if Input.is_action_pressed("ui_down"):
        $AnimatedSprite.play("Duck") 
        $Normal.set_disabled(1)
        $Duck.set_disabled(0)
    elif $DuckDetection.(I_dont't know what to put here)(): 
        $Normal.set_disabled(1)
        $Duck.set_disabled(0)
    else:
        $Normal.set_disabled(0)
        $Duck.set_disabled(1)

SIsilicon | 2018-10-12 17:00

:bust_in_silhouette: Reply From: SIsilicon

What you can do is use ray casting. But contrary to the link I gave, RayCast2D can be your DuckDetection node instead of using the 2D space directly. The RayCast2D’s cast_to property defines the direction the ray is casted and to the maximum distance (guess it’s more like a line segment). So for you, cast_to should be pointing up and length must be tweaked to what your game needs. Also, enable exclude_parent.

Now in code what you must do is find whether the DuckDetection node is colliding with something.

 #The method you need in the second if
 If $DuckDetection.is_colliding():
     ...

Thanks, it worked, surprised at how fast you answered

VilmakerStudios | 2018-10-12 22:55