+2 votes

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.

in Engine by (21 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

by (28,789 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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.