+1 vote

Hi, I want to get a list of bodies inside an Area2D (or any other node, but I thought an Area2D was the correct one). I was trying to use getoverlappingbodies() but I get 0 elements every time. I don't know if this function does something else or maybe I am using it wrong. Or maybe there is another way to do it.

asked Dec 25, 2016 in Engine by epord (20 points)

Are you trying to detect PhysicBody2D overlaps just after adding the Area2D to the tree or the area was already there?

I have my character and I want to get all bodies near him. I have an Area2D following him and when I press a key I want to get all bodies in that area.

Then, get_overlapping_bodies should work...

Can you specify your collision layers, masks, of the area and bodies, also the structure in the scene (of the area and the undetected bodies)?
And show the portion of code that is not working.

I found the issue. Apparently a have to use a CollisionShape2D with the Area2D. Now it's working fine, thanks!

1 Answer

+1 vote

If get_overlapping_bodies gives you no bodies, you should check that have at least one shared collision layer, and the Area2D has to be monitoring http://docs.godotengine.org/en/stable/classes/class_area2d.html#class-area2d-set-enable-monitoring

An alternative way to detect bodies is connecting methods to one of these signals:
http://docs.godotengine.org/en/stable/classes/class_area2d.html#signals

answered Dec 25, 2016 by Zylann (27,009 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.