How to detect whether an object has entered an area?

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

The object is in type of “StaticBody2D”, and as for the area , i plan to use “Area2D”.

Do you know how to detect when the object enters the area in codes?

I’m new to godot. Thank you!

:bust_in_silhouette: Reply From: Zylann

StaticBody2D is not meant for an object that can move, I suggest you use KinematicBody2D or RigidBody2D.

Area2D has a signal you can connect to, body_enter: Area2D — Godot Engine (latest) documentation in English

You can connect it from the Node panel (or in code with connect()) to a method of your script and it will be called whenever a body enters the area.

Thank you. I followed the tutorial from here which is very long to read. Mostly I created an Area2D node, then added a subnode “CollisionShape2D”, and I adjusted the shape within the area I want to take care of in the scene file ( say “my_scene.xml”) .

And my codes are here:

Imgur: The magic of the Internet

enter image description here

Honestly I am a little confused about the doc because it doesn’t have examples in details like charts or images. But it is helpful. Thank you!

jake_anderson | 2016-12-15 16:37

With an area, you can use get_overlapping_areas and get_overlapping_bodies inside fixed process code too.

http://docs.godotengine.org/en/stable/classes/class_area2d.html#class-area2d-get-overlapping-areas

eons | 2016-12-15 18:29