Having Area2Ds in different scene instances interact

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

I’m trying to make a platform fighting game, and I’m using area2Ds as hurtboxes and hitboxes, under a KinematicBody2D, which I’m using as the character. The KinematicBody2D is it’s own scene, so I can make multiple instances of it. I need the Area2Ds uto be able to detect each other across different instances of the character. Is this possible?

:bust_in_silhouette: Reply From: Wakatta

Once they are added to the sceneTree then its mighty possible

Connect the Area2D’s signals body_entered(body) and body_exited(body) to your script or call the get_overlapping_bodies() function from the Area2D node.

func _on_body_entered(body):
    do_something_with(body)

func _on_body_exited(body):
    stop_doing_something_with(body)

And alternate approach, you could add each KinematicBody2D instance to a group and iterate the group to find each member’s distance from another and react accordingly

Sorry, how do I add the Area2Ds to the main scene tree so that the they can interact?

from cleveland | 2020-12-11 08:38