- Home /
Using shadows as Triggers?
Hi,
At the moment I am working on a new game of mine, with the overall idea in a nutshell being that you have to stay in the shadows and not get caught. Almost all of the game is complete, minus the crucial part, being that when you step out of the shadows in to the light you are caught. Is there any possible way (through scripting) that you can have the shadows/light acting as some form of 'Trigger' or is the only solution the long winded process of placing individual colliders everywhere?
Thanks
Cast a ray from the light source to the centre of the object or to its renderers.bounds corners, can you? The shadow casters should have their colliders enabled.
Firstly, thanks for the quick reply. The idea suggested sounds like it should work and I'll get started to try and figure it out. I'll let you know how I get on. Again thanks for the answer.
Answer by Pangamini · Jan 14, 2014 at 07:40 PM
I first thought about light probes, but you probably want to have dynamic lights and having only certain points raycast checked may fail (what if corners of AABB are in the shadow, but really just corners?) And then i got this idea: to use rendering to detect light in the area. I won't give you an exact solution, but i don't see a reason why this shouldn't work. So:
Create a camera with black background. This camera will be set to filter everything but an object you will use as a light sensor. Could be cube or capsule of a player size, or the player object itself. But rendered with a special shader, that renders both front and backfaces, with additive blending (and lighting enabled)
This camera will look at the sensor object from closeup (so it takes most of the "screen") render into a texture (very small resolution is enough). The camera will also do a very strong blur on the postprocessing, so strong so the whole texture gets almost average color.
Then, simply get the renderTexture and get pixels out of it (since the color is almost average everywhere, only one or few, spread across its surface, will be enough). Average these colors and you will get some data about how much your character is lit.
Another modification would be to render the light sensor with special lighting shader (no surface shaders for lighting) so that you would calculate detection threshold per pixel, and make the pixels either completely white or black. In this case, the pixels you pick from the texture would have to be anything above zero and you'd safely say the sensor was lit over the threshold intensity somewhere.
This type of detection would work mesh-wise.
Again, i've never tested anything like this.
Thanks for that answer. It seems very in depth, I gave it a go and could not get it to work. Since then I have changed the overall concept of the game (through different reasons, not giving up!) and therefore will not be needing this anymore. Once again thanks for your help
Your answer
Follow this Question
Related Questions
Trouble with light switch 3 Answers
Colliding two GameObjects 1 Answer
OnTriggerEnter is not running! Need help with collision. 2 Answers
Trigger Spawning? 1 Answer