Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
1
Question by SuperRaed · Sep 23, 2016 at 08:37 AM · raycastlightshadowdirectional light

How to detect if an object is under a shadow casted by a directional light

I'm assuming we can't treat a directional light as any other form of light, that is as a single point, but as a plane with the same orientation as the directional light. so instead of casting a ray from my object towards the center of the DirectionalLight, I'm casting it from myobject.transform.position to myObject.transform.position - directection of DirectionalLight so that I'm casting lines parallel to the light as in the picture

alt text

and if the ray hits a wall on it's way then set a bool value underSun= true else underSun=false and to increase the accuracy of the detection, instead of casting a single ray from the center of the object ,I'm casting four each from the it's top corners alt text and on everyUpdate if a rayCast from any of these corners doesn't hit a wall then break and set underSun= true

this does make sense to me, but I'm getting some weird unwanted results with my code and I would appreciate if anyone can point me to the right direction. see in gif below

wanted behaviour

Unwanted Behaviour

the code is here

 using UnityEngine;
 using System.Collections;
 
 public class NewPlayer : MonoBehaviour {
 
    
     public NewSun sun;
 
     private MeshRenderer mesh;
     private RaycastHit hit;
     private bool underSun = false;
     
     // Use this for initialization
     void Start () {
         mesh = GetComponent<MeshRenderer>();
     }
     
     // Update is called once per frame
     void Update () {
         underSun = false;
         Vector3 sunDir = sun.transform.forward;
         sunDir.Normalize();
         sunDir *= 100;
    
         foreach (Transform child in transform) {
 
             if (!Physics.Raycast(child.position, child.position - sunDir, 30, LayerMask.GetMask("Wall")))
             {
 
                 Debug.DrawLine(child.position, child.position - sunDir, Color.red);
                 underSun = true;
 
             }
             else {
                 Debug.DrawLine(child.position, child.position - sunDir, Color.green);
             }
            
         }
 
        
 
         if (underSun)
         {
             mesh.material.color = Color.red;
         }
         else {
             mesh.material.color = Color.green;
         }
        
 
     }
 }
 
 



planlight.png (19.4 kB)
planlight2.png (3.5 kB)
Comment
Add comment · Show 8
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 tanoshimi · Sep 23, 2016 at 08:48 AM 0
Share

That's your actual, complete code? Where is "sunDirection" defined?

avatar image SuperRaed tanoshimi · Sep 23, 2016 at 08:52 AM 0
Share

sunDirection =sunDir now , it's basically the transform.forward vector of the directional light which is assigned publicly

avatar image tanoshimi SuperRaed · Sep 23, 2016 at 09:05 AM 1
Share

What you had written before generated a compile error - if you're asking for help with code, you really need to write exactly the code you were using :)

So your logic seems sound but your colour coding is a little tricky to follow: a green debug line of the raycast indicates that no collider has been hit, whereas a green coloured cube means that at least one collider has been hit, right? And your "bad behaviour" video seems to indicate that all four raycasts are hitting something.

$$anonymous$$ay I suggest you change your logic to use Debug.DrawLines to show all the raycasts, then colour them red if they hit something and green if they don't? You'll find it much easier to visualise. You can then use one of the other overloads of Physics.Raycast() that outputs a hitinfo of the collider that was hit and print its name, and the position at which it was hit.

Show more comments
avatar image Bonfire-Boy · Sep 23, 2016 at 09:32 AM 0
Share

Shouldn't the second parameter to RayCast just be the direction ie -1f * sunDir ?

avatar image SuperRaed Bonfire-Boy · Sep 23, 2016 at 09:40 AM 1
Share

I think that would converge the rays towards a point ins$$anonymous$$d of casting parallel rays

avatar image Bonfire-Boy SuperRaed · Sep 23, 2016 at 10:03 AM 1
Share

No, that would be true if the second parameter were an end point for the ray, and the OP is using it as if that's the case. It's not, it's a direction. So it needs to be the same for all the casts in order for them to parallel.

1 Reply

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

Answer by tanoshimi · Sep 23, 2016 at 09:55 AM

Having read @Bonfire-Boy's comment above, I think I've spotted your problem. I'm not sure your Debug.DrawLines are actually visualising your Raycasts correctly. Take a careful look at the function signatures:

  • In Physics.RayCast(), the first two Vector3 parameters are an origin and a direction.

  • In Debug.DrawLine, the first two Vector3 parameters are an origin and an end position.

So, for Debug.DrawLine you're correct to use child.position - sunDir but, for RayCast, you should be using -1f * sunDir as he suggests.

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 Bonfire-Boy · Sep 23, 2016 at 10:03 AM 1
Share

You've got an extra - in there.

avatar image SuperRaed Bonfire-Boy · Sep 23, 2016 at 10:08 AM 0
Share

@Bonfire-Boy true

avatar image tanoshimi Bonfire-Boy · Sep 23, 2016 at 10:13 AM 0
Share

Aye, corrected - thanks!

avatar image SuperRaed · Sep 23, 2016 at 10:05 AM 0
Share

That's what the problem was, it works at last ,thanks a lot guys :)

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

75 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 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 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

Keep Shadows From Being Lit Up 1 Answer

How Can I Detect If An Object Is Lit By A Specific Light? 1 Answer

Accessing a shadowmap and using it in C# script to turn off colliders? 0 Answers

Weird Shading Thing (Directional Light) 1 Answer

No shadows. 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