Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 YeOldeSnake 1 · Aug 28, 2010 at 09:35 AM · gameobjectlight

How to detect if a GameObject is hit by a light

How to detect if a GameObject is hit by a light

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
4
Best Answer

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.

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
avatar image
1

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.

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 YeOldeSnake 1 · Aug 28, 2010 at 11:52 AM 0
Share

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

avatar image Ashkan_gc · Aug 30, 2010 at 05:38 AM 0
Share

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

avatar image
0

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.

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

No one has followed this question yet.

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

Ammount of Light received by a GameObject 1 Answer


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