How can I detect if the player is in the darkness?


I am currently working on a horror game, and I am trying to slowly increase a light’s intensity when the player is in the dark. That should give the player a feeling like their eyes are getting used to the darkness, like they do in real life. The problem I came up with, was how to detect if the player is in the darkness. I have set up lights with shadows, so I would like to do something more advanced than just picking a light’s range and detecting if the player is in it, cause the lights sometimes reach other rooms that are dark only because of the shadows.

Can I do it for example with detecting the material’s color (IF the lights affect the material color…)?

I ended up using RayCast from every light to the player. I have also set up a boolean, which tells the player if he is in the dark/light. If the player is not hit by the RayCast, it means that the player is in the dark, otherwise, the lights that hit the player with the ray send a message to the script to turn a boolean on.

Just answering in case it helps someone… :wink:

Its a tricky problem, theres a similar discussion going on [in this thread][1]. My feeling would be that the easiest way to do it would be to write a custom class that sits on your light source and checks if its hitting the player using raycasts. Basically faking it will probably be much less complex then sampling shaders. Might also want to look at how they do the [lighting in Shadowgun for some examples][2]

Hope this helps!
[1]: Detect light/shadow falling on object? - Unity Answers
[2]: Advanced Shading and Lighting for Mobile | Unity Blog

You could try adding invisible ghost boundaries in the dark areas. When the player enters these boundaries, a variable is activated, causing the brightness the change. Leaving the bounds would obviously reverse the effects.