- Home /
How to detect if a GameObject is hit by a light
How to detect if a GameObject is hit by a light
Answer by jtbentley · Aug 29, 2010 at 02:11 AM
This depends on how accurate you wish to be.
The fast method would be to do a Vector3.Distance(light to obj), but that won't take shadows into account.
A slower method would be a single raycast from the object's pivot to the light, if it hits a collider in the middle, you would consider it in shadow.
Slower again would be casting a ray from the render-extent's points (ie, from all the corners of the bounding box), and you would then need to decide just how much light needs to be visible before you trigger (the bounding box has 8 points, if 3 of those come back blocked, is that hidden or not?).
Definitely check out the manual for Raycast area. You can cast quite a lot of rays per frame before you hurt performance, I cast about 6 per frame on an iPhone game with no apparent cost. There's also no reason that you would have to perform this check every frame, either.
Answer by Ashkan_gc · Aug 28, 2010 at 10:11 AM
you can use a trigger in size of your light if the light is a pointlight but if you are talking about other types you should trace a ray from the source to see if it's hitting or not. even advanced lighting/rendering computer programs do this but this is not a good idea to cast many rays in each frame so use triggers and colliders if possible.
in OnTriggerEnter you can check if there is something between them or not by tracing a few rays.
in unity and most other runtime applications all objects near the light source will be effected by the light even there is a wall between them. turn shadows off to see what i say.
yeah , well i was asking about some answer which would apply for all the lights without me having to work on em but ok that kinda worked , ill be waiting if someone else has an answer
all objects are always hit by directional lights. for point lights as i said before, you can use triggers (sphere ones) or raycasts if you need to see something is between them or not. for spotlights you should calculate the frustom. i am not good at math. :)
Answer by Kroltan · Apr 04, 2011 at 09:14 PM
A not-so-efficient way that considers shadow is projecting a ray from the light right into the position of the object you want to check, and, as said above, if the ray hits anything else than the collider you want, cinsider it in shadow.
Your answer
Follow this Question
Related Questions
Accessing lights from c# code 3 Answers
Point Light not being destroyed with its parent GameObject? 0 Answers
Lights on floors! 2 Answers
How to make a game object light up only when its clicked on 2 Answers