Question about get_overlapping_areas(), returns empty array if...

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

Hi,
I’m making a 2d fps minigame, the way it works it’s pretty simple:
When the player “shoots”, it instances a bullet scene, which is an area2d.
The bullet then checks, in _physics_process, if it’s inside another area2d using get_overlapping_areas(), which returns an array. The bullet then queue_frees itself if hits or after a period of time.
The first object of the array gets “hit”.

Here’s the problem:
At first, when instancing the bullet scene, I made it a child of the root node (using level = get_tree().root.get_child(0), level = get_node("root/level) or any other method then level.add_child(new_bullet)), but then the overlapping areas’d return an empty array, even if it’s inside the target.
However if I made the bullet a child of the player, with self.add_child(new_bullet), then the function returns the array with the target inside.

The problem is solved, but I still wanna know why the first method didn’t work, did I get the root node incorrectly?
The layers are setup correctly, since the 2nd method worked.
Ty

:bust_in_silhouette: Reply From: Inces

It should work. Are You sure bullet was really inside the target ? Because if You make something a child of the root, it usually spawns at Vector2(0,0) - as it is root position or root does not have position property at all.
Or perhaps You made new variable using reference to overlapping areas on bullets ready, and it was not correctly updated in process ? If You are interested about that, show bullets code here :slight_smile:

Last thing - You might have solved problem, but childing a bullet to player will soon provoke more problems. You will notice, that bullet jumps with the player and rotates with the player, I doubt You want such behavior :slight_smile: