get_collision_bodies() retruns <Array([])> on RigidBody2D even though contact monitor is turned on

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

Hello everyone!

I am trying to get collision on a RigidBody2D.
However it keeps returning <Array()>
I already turned on contact monitor and set Contacts Reported to 1

it still returns <Array()> however.

What am i doing wrong?

i am using godot3.0.6

:bust_in_silhouette: Reply From: blank

If you read the documentation for get_colliding_bodies() function, you’ll realize it’s not the best suited function to use to detect collisions for a RigidBody2D, and that it is working as intended by returning an Array of colliding bodies. I’d strongly advise you to use either RigidBody2D’s body_entered and body_exited signals or directional raycasting, e.g. having multiple RayCast2D nodes, and checking their collisions each physics tick. Or additionally use Area2D that has a slightly bigger cillision than your RigidBody2D and it’s body_entered and body_exited signals. Also set-up physics layers, so you can use collision layers and collision masks to avoid checking for collisions where it’s not needed.

It’s hard to give you the right answer without really knowing what is your intention and what kind of collision are you checking for. You can read up more about your use case here RigidBody2D documentation.