How to detect a collision between Sprite and Light2D?

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

I’m developing a game that has a mechanic where the user uses always a flashlight on dark environments, but when it enters a room with light I want to deactivate the flashlight.

Since the Light2D doesn’t have collisions or a body_enter() signal, how can I achieve this?

:bust_in_silhouette: Reply From: Zylann

You can add an Area2D as child of the light with a collision shape of proper dimensions, so you will be able to tell if you entered in a lit area.

If you use scenes that are either all lit or not, another way is to just check if there is any light in the scene. That can be achieved by either traversing the scene tree, or by putting your light in a group and check if that group has any light.

I think in that system you would have to somehow make the area2d polygon match the shape of the occluded light, so It would not work

bobojo | 2023-03-07 21:14