How to make a KinematicBody2D check whether an area is empty.

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

I’m making a 2D platformer with an enemy that moves left and right.

It already changes direction when facing a wall, but I would like it to change directions when it’s about to fall off from a platform by checking the area near and below it.

Something like this:

You can use the test_move() function to detect the spaces in front and the back of an enemy.

Ertain | 2021-01-21 17:59

:bust_in_silhouette: Reply From: cascas

You could have a raycast in front of the kinematic body checking if there is a floor where it is moving towards.

I tried, but the enemy has a wide capsule collision shape.

Kinball | 2021-01-21 17:18

You can use the raycast’s add_exception method for all your enemies.

cascas | 2021-01-21 18:03