- Home /
How can I detect if the player is in the darkness?
Hi.
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...)?
Answer by LukaKotar · Aug 14, 2012 at 03:23 PM
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... ;)
Rays are a foreign concept to me, can you explain how to create a Ray, and what it's functions are?
Look at the reference for Physics.Raycast: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
Here is what I've done:
I've created a variable on my player's script, (boolean) and it is a "public static" one, so other scripts can access it.
var player : GameObject; var range : float; public static var playerIsInLight : boolean;
function Start(){ player = GameObject.FindGameObjectWithTag("Player"); range = this.light.range; }
function Update(){ var dist = Vector3.Distance(player.transform.position, this.transform.position); var hit : RaycastHit; var rayDirection = player.transform.position - transform.position;
if(Physics.Raycast (transform.position, rayDirection, hit, range)){
if(hit.transform.tag == "Player"){ playerIsInLight = true; } }else{ playerIsInLight = false; } }
and in the other script I check if it is true, so you would look for the reference for GetComponent: http://docs.unity3d.com/Documentation/ScriptReference/Component.GetComponent.html ... and check if it is true.
Sorry, I don't know how to format the code in the comments :\
Thank you for the explanation! I will have to toy with it some, but this gives me a good idea where to start.
O$$anonymous$$, no problem. If you have any questions just ask. ;)
Answer by dylanfries · Jun 29, 2012 at 12:29 PM
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]: http://answers.unity3d.com/questions/24662/detect-lightshadow-falling-on-object.html [2]: http://blogs.unity3d.com/2011/06/08/advanced-shading-and-lighting-for-mobile/
Nice idea - or the other way around - check from the player to the lightsource if it is within a certain range.
Answer by StUpidxCrAzy · Jun 29, 2012 at 01:34 PM
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.
Your answer
Follow this Question
Related Questions
shadow distance per light(Directional)? 1 Answer
Can particles cast real time shadows on other objects? 1 Answer
How can I render a shadow for an obstacle, but without rendering the light on the obstacle itself? 2 Answers
I am trying to learn Survival Shooter (Tutorial Project) 0 Answers
What is causing these lighting issues? (with picture) 1 Answer