Check if a objekt is in a Area2d

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

I have an Area2d and I have multiple KinematicBody2ds. Some of them are in a group called "Enemy5" and Some are in a group called "Enemy9". Now I have a function in a script attached to the Area2d func Check(): and in this function I want to check how much KinematicBody2Ds which are in one of those groups are in the Area2d.

:bust_in_silhouette: Reply From: estebanmolca

You can use the signals: on_Area2D_body_entered() and on_Area2D_body_exited()

func _on_Area2D_body_entered(body):
    if body.is_in_group("Enemy9"):
        Enemy9_count + = 1
    if body.is_in_group("Enemy5"):
        Enemy5_count + = 1
    
    
func _on_Area2D_body_exited(body):
    if body.is_in_group("Enemy9"):
        Enemy9_count - = 1
    if body.is_in_group("Enemy5"):
        Enemy5_count - = 1