Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by LukaKotar · Jun 29, 2012 at 11:02 AM · lightshadowsdarkness

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...)?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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... ;)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IndieScapeGames · Aug 14, 2012 at 03:34 PM 0
Share

Rays are a foreign concept to me, can you explain how to create a Ray, and what it's functions are?

avatar image LukaKotar · Aug 14, 2012 at 03:47 PM 1
Share

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 :\

avatar image IndieScapeGames · Aug 14, 2012 at 04:03 PM 0
Share

Thank you for the explanation! I will have to toy with it some, but this gives me a good idea where to start.

avatar image LukaKotar · Aug 14, 2012 at 04:29 PM 0
Share

O$$anonymous$$, no problem. If you have any questions just ask. ;)

avatar image
1

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/

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Jun 29, 2012 at 12:33 PM 0
Share

Nice idea - or the other way around - check from the player to the lightsource if it is within a certain range.

avatar image LukaKotar · Jun 29, 2012 at 03:38 PM 0
Share

Thanks for the answer! Checking out the sites now ;)

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Directional Light Shadows With Cookies 1 Answer

Pitchblack inside, lighter shadows outside? 0 Answers

Multiple directional lights with shadows 1 Answer

Doesnt work shadows in build using Dual Lightmaps 0 Answers

How can i make the game area pitch black 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges