Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 EricGomesGregory · Apr 10, 2017 at 07:49 AM · c#raycasting

Coding Question, Can you detect shadows

I'm trying to build a game that revolves around stealth. To do so I would like to know if is possible to use ray-casts to detect shadows. If so can someone show me a example?

Thank you for answering!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by tanoshimi · Apr 10, 2017 at 07:53 AM

Shadows are cast on a surface because light from a light source is blocked by some opaque object.

So you can tell whether any given point is in shadow by firing a raycast from that point towards the light, and checking what the first object it hits is. If the raycast hits the light, it's not in shadow. If it hits anything else along the way, it's in shadow.

Comment
Add comment · Show 3 · 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 Kossuranta · Apr 10, 2017 at 08:11 AM 0
Share

As he is asking a method to detect shadows I think what he wants is for example patrolling enemy could detect shadows, which would make really cool gameplay elements for s$$anonymous$$lth games.

avatar image tanoshimi Kossuranta · Apr 10, 2017 at 08:34 AM 2
Share

And that's what I've supplied - you cast a ray from any point on any surface to the light source to deter$$anonymous$$e if it's shadow or not. How you use that information to inform the behaviour of an enemy AI is then up to you.

avatar image UnityCoach · Apr 10, 2017 at 08:41 AM 0
Share

Usually, you Raycast from the NPC toward the Player to find out if the Player is in sight. Often, you also use other states, such as "is the player hidden in bushes" which you can easily do using Terrain data, or "is the player hiding" to limit the odds the NPC sees it. You also want to control the NPC "field of view", using some dot product usually does the trick.

In your case, you want to know if the Player is casting a shadow that the NPC can see. So, to avoid casting so many rays, you could cast a ray from the light(s) toward the main parts of your Player (head, torso, hands, feet), then from the point it hits, ins$$anonymous$$d of Raycasting again, you could use some math involving world normal, dot product, distance and arbitrary body part size and tolerance to tell if the NPC can see it or not. Same thing with Reflections actually, using Reflect.

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

9 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Raycast problems 1 Answer

Find closest point on all objects in a layermask 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