Signal when Position2D enters Area2D

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

In my game, the world is divided into multiple rooms, each with an Area2D that encapsulates its bounds. I want to be notified (preferably via signals) when a specific point (e.g. Position2D) enters each rooms so that I can keep track of where my player character is and react accordingly with e.g. the camera.

I’m currently using the character’s CollisionShape2D and Area2D’s body_entered signal to achieve this, but it would be better if I could know when the center of the player enters the room, rather than when the edge of the player does so.

Is this possible without having to e.g. constantly query each room and check whether the player’s current position lies within the bounding box?

Thanks in advance!

Couldn’t you use a smaller collision shape inside the player just for this? By using the Layer and Mask properties you could avoid the area to detect the bigger shape.
Just what came to my mind :confused:

Jowan-Spooner | 2019-05-24 09:04

I considered this, but I really would prefer it to be a zero-dimensional point, rather than a two-dimensional shape, so that it would be impossible for the character to exist in two rooms at the same time.

AUD_FOR_IUV | 2019-05-24 15:33

If size of the RectangleShape2D is Vector2(0.5, 0.5)(1 pixel) , Player cannot be in two separate rooms at the same time. Pixel snapping should be turned on and rooms should be in integer size. Then You can know which room you are in by using one of the area_entered(), area_shape_entered(), body_entered() signals. The size can be adjusted from the editor, or with script;

extends CollisionShape2D

func _ready():
	shape.extents = Vector2(0.5,0.5)

razah | 2020-10-04 23:09