Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
0
Question by miszelfox · Jul 17, 2015 at 07:31 PM · lightinglightshadowsystemdarkness

Darkness system

Is there a way to check if mesh/object is covered by shadow or something like that? I would like to achieve effect that you can hide in shadows, or hide bodies. I would prefer to do it realtime - not by predefinied areas, because i would like to include flashlights etc.

Comment
Add comment · Show 4
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 Positive7 · Jul 17, 2015 at 07:59 PM 0
Share

Raycast from the light source to the player or the object to hide. If light hits something before the object than it's in shadow.

avatar image miszelfox · Jul 17, 2015 at 08:06 PM 0
Share

It might work but i would have to include additional things, like spotlight angle and distance. I'd also have to add some kind of variable which counts number of lights that cover player and if its value is 0 then player/object is hidden.

avatar image Positive7 · Jul 17, 2015 at 09:24 PM 0
Share

Something like this should do for a start

 using UnityEngine;
 using System.Collections;
 
 public class LightCheck: $$anonymous$$onoBehaviour {
 
     public new Light[] light;
     public GameObject[] go;
     public static bool inLight;
     void Start () {
         go = GameObject.FindGameObjectsWithTag("Shadowed");
         light = (Light[])FindObjectsOfType (typeof(Light));
     }
 
     void Update () {
         RaycastHit raycastHit;
         foreach (GameObject i in go) {
             foreach (Light l in light) {
                 Vector3 rayDirection = i.transform.position - this.transform.position;
                 if (Physics.Raycast (this.transform.position, rayDirection, out raycastHit, l.range)) {
                     if (raycastHit.transform.tag == "Shadowed") { 
                         inLight = true;
                     } else {
                         inLight = false;
                     } 
 
                 }
             }
         }
     }
 }

and then

 if (LightCheck.inLight) {
 //Do Something
 }else{
 //Do Something else
 }
avatar image miszelfox · Jul 18, 2015 at 12:56 PM 0
Share

Thanks for help, but what should i do with directional lights? It might not work this way.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by calmcarrots · Jul 17, 2015 at 08:00 PM

Use light probes

http://docs.unity3d.com/Manual/LightProbes.html

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 miszelfox · Jul 17, 2015 at 08:07 PM 0
Share

And what should i do with them? Is there a way to check if player is covered by shadow using them? If yes, then how?

avatar image calmcarrots · Jul 17, 2015 at 08:37 PM 0
Share

Did you look at the link I gave you? Read it all. Then look at the scripting options you have for light probes. They're dead simple and I don't think it requires much explanation. Do some more research on light probes. But no one is going to code a whole entire darkness system.

http://docs.unity3d.com/ScriptReference/LightProbes.html

Also, here are some extra links

https://www.youtube.com/watch?v=O3DT1QDGelo https://www.youtube.com/watch?v=eGu9_8HS2uI

avatar image
0

Answer by Lahzar · Jul 18, 2015 at 06:37 PM

Are you doing this in 2D or 3D? 2D is much easier to do this.

I use something similar in my AI, but its not exactly perfect yet. I have a rendertexture which renders the player and a shadow layer from a camera placed on the AI. Every X seconds (I use 0.1) it starts a coroutine which gets the rendertexture and converts it to a texture and then convert that to a 2D array of colors. Then I have a thread which compares the grayscale value of every single pixel in said array to a predefined color set in the inspector. And then it uses some more advanced stuff to change how my AI reacts, but thats seems offtopic for this. The shadow layer is the entire map copy-pasted to a new layer and with all the mesh renderes turned to shadows only, so they wont render, just cast/receive shadows.

It works great and I have not noticed any performance drops, but its not perfect. Im not a lighting expert, so my scene is very poorly lit, just 1 directional light and a few point lights here and there. So when the player stands between the AI and the player, the AI doesn't see the player because all the light hits the back of the player... I am in the process of trying to fix this, but you know...

It might not work perfectly for your scenario, but its atleast a reasonable dynamic shadow detection system.

 //Made by Imre Angelo aka Lahzar on the unity forums
 //You should be able to fill in the blanks/predefined variables
 //Also remember to play around with ambient occlusion. Can make things better & easier! Enjoy!
 
 static bool processing = false;
 
 void Start()    {
         #region Create RenderTexture
         RenderTexture template = GetComponentInChildren<Camera>().targetTexture;
         rt = new RenderTexture(template.width, template.height, template.depth);
         rt.Create();
         GetComponentInChildren<Camera>().targetTexture = rt;
         #endregion
 }
 
 #region Shadow Detection
     IEnumerator RenderToTexture()    {
         processing = true;
         yield return new WaitForEndOfFrame();
 
         RenderTexture.active = rt;
         text = new Texture2D(rt.width, rt.height);
         text.ReadPixels(new Rect(0,0,rt.width,rt.height),0,0);
         text.Apply();
         pixels = text.GetPixels();
         DebugMaterial.mainTexture = text;
 
         yield return new WaitForSeconds(freq);
 
         Thread t = new Thread(ComparePixels);
         t.Start();
         t.Join();
         processing = false;
         yield return visible;
     }
 
     void ComparePixels()    {
         foreach(Color p in pixels)    {
             if(p.grayscale >= bright.grayscale)    {
                 visible = true;
                 break;
             } else {
                 visible = false;
             }
         }
     }
     #endregion



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

25 People are following this question.

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

Related Questions

Light creates distortion on mesh? 1 Answer

No shadows. 2 Answers

Light with minimum bias not sufficient 1 Answer

Casting sharp 2D shadows side on 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