Area2D has a get_overlapping_bodies
method. According to the docs:
For performance reasons (collisions are all processed at the same time) this list is modified once during the physics step, not immediately after objects are moved. Consider using signals instead.
I personally use a different approach, which appears to work well, and seems to be more performant:
- Keep a list of overlapped bodies in a local variable like
_overlapping
- Add a signal to
body_entered
that appends the body to _overlapping
- Add a signal to
body_exited
that removes the body from _overlapping
Each frame (in _process_physics
), act on everything in _overlapping
, e.g.:
func _process_physics(delta):
for body in _overlapping:
body.take_damage()