How to detect a collision with an area2d?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bugsbane
:warning: Old Version Published before Godot 3 was released.

I’m trying to detect if any object is colliding with a specific area2d I’m using as a hit box. Currently I’m just using is_colliding, however I keep getting an error:

Invalid call. Nonexistent function ‘is_colliding’

Yet I see is_colliding used in example tutorials all over the web, and the only difference I can see is that they’re attaching it to a kinematic character2d. Can this be used with area2d? Is there a better way? Here’s my code:

extends Area2D

func _ready():
	set_fixed_process(true)

func _fixed_process(delta):
	if(get_node(".").is_colliding()):
		print("Collision!")

no need to use get_node("."), since you’re already in the object you can use self.is_colliding(), or even simply is_colliding().

Dr_TwiSteD | 2017-07-21 12:44

:bust_in_silhouette: Reply From: Dr_TwiSteD

Areas are not ‘colliding’ because they are not ‘physical objects’. Instead a body or an area can ENTER or EXIT a certain area.
You should use corresponding signals to react. You can connect to the signals on the signals tab or from within the code.

in such case the code would be (for a default Area2D):

func _on_Area2D_body_enter(body):
    print(str('Body entered: ', body.get_name()))

A specific body can be detected easily with the help of groups:

func _on_Area2D_body_enter(body):
    if body.is_in_group("player"):
        print(str('Player has entered')

The body that needs to detect the area could have a child area for overlap detection too.

Sometimes is better to have a sub-structure that is functional to a specific purpose (but is a design decision).

eons | 2017-07-21 13:21