How to create a “mental health” bar that decreases when the player is in the dark and stops when there is light ?

Hello, I’m currently developing a horror game for my school. I would like to know how to create a “mental health” bar that decreases when the player is in the dark and stops when there is light. Thanks

You can define trigger boxes in the dark area of the game, whenever player enter it, player start draing its metal heath.

I have a raycast based approach, just shoot a ray from the player towards every light in the scene one by one using a for loop, if the ray hits a light then break the loop and dont decrease mental health else decrease mental health, using a layermask and a special layer for lights, make it so that the ray can only interact with lights, make sure the lights have colliders

Heyho!
Depending on what is most efficient for you, you should decide what to do, one idea is:

Have a float decrease in value over time, while a bool inLight is false.
Set this bool to true when you are inside some light.

The most simple way to do this is trigger colliders.

Check the unity registry or search on youtube regarding visualizing this value as a slider

Light level determination for gamepley logic is an interesting problem and not an easy one necessarily.

You can create an additional Camera that renders to very low resolution Render Texture what the players sees using a super basic Vertex Lit (white color + ambient + lighting, no textures maybe?) for opaque geometry and ignores translucent geometries.

Alternatively: you can use Unity - Scripting API: Camera.RenderToCubemap to render whole surrounding of the player.

You can then run a basic image analytics program once or twice a second (non-main CPU thread) on these pixels to determine brightness level of what player sees at the moment and use this single number in your gameplay logic.