- Home /
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!
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.
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.
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.
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
Follow this Question
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