- Home /
Enemy got lit by flashlight detection
Hi there, so im developing some 3d horror game and i need to check if enemy got lit by player's flashlight. Raycasting from camera.transform.forward isnt a solution cause it dont full represent flashlight's light cone form. Any ideas?
A flashlight's area of effect is a cone, so if you wanna know if an object gets hit by it, you're basically asking if the object is inside or outside of the flashlight's cone.
There's code to check this here:
http://stackoverflow.com/questions/10768142/verify-if-point-is-inside-a-cone-in-3d-space
It requires you to deter$$anonymous$$e some input point, however. $$anonymous$$aybe do it once for each of the corners of the enemy's bounding box?
Alternatively, you could raycast a number of times with the flashlight as the origin and aimed in uniformly distributed places around the area of the cone's circular bottom. The number of times can be considered a 'sample density' and should depend on the enemy's size such that enough rays are cast that the enemy can't possibly get missed, if inside. If the flashlight has a sufficiently narrow area of light, or sufficiently short range, it might not have to be that expensive.
Oh thanks! The second method is really a solution. Just need to add musltiple raycasts on different angles... still, i think i can handle it.
By the way, first method is not for situation like this — a wall between player and enemy would still trigger the collision. Thanks for suggestion anyway mate!
Answer by Jessespike · Mar 24, 2015 at 03:02 PM
Have you tried a SphereCast? I know it's not a cone. But it might be close enough for what you're trying to simulate.
Thanks, but thats not what am i looking for. Sperecast would not collide enemy in weird ways — just like a situation when enemy is behind a thin pillar. In that situation enemy would be visually lit, but the event would not trigger.
Your answer
Follow this Question
Related Questions
Multiple/Different footsteps sounds (solved) 2 Answers
Create interchanging Raycast cone for flashlight 2 Answers
Capturing a point 2 Answers