- Home /
Collision Detection on Spotlight
Hello, I am creating an enemy that is holding a flash light. I only want the enemy to persue if they are in the light of the flash light. What do you think is the best way to do this? I thought of having a raycast coming off the enemy so that if they made contact with the raycast the enemy would persue. However this has issues because raycasts can go through walls and i don't want the enemy to be able to see through walls. I would also just but a collision detector on the spot light itself which i am having eminate from the flash light, however the size of my spot light is different obviously to the size of light that it puts on the ground so it wouldn't be accurate either.
Suggestions?
Answer by syclamoth · Oct 14, 2011 at 02:44 PM
Well, to begin with raycasts only go through walls if you specifically tell them to, so that isn't really a problem.
What you need to do here, is remember that the 'spotlight' is not actually the thing that allows the enemies to see you- it is, in fact, a signal to the player that they will be visible if it passes over them. With this in mind, just forget all about the light for a moment and treat it just like a standard "view cone" problem. There are a few ways of managing this problem- you could draw a line between the player and the enemy, and detect both whether the player is in view, and whether the line is at the correct angle from the direction the enemy is looking in.
One rather nice solution to this problem that I have seen, is to fire off one (or more) random raycast every fixed update, from the enemy's eye point to a random position on your player's model. That way, with a combination of 'view cone' and occlusion testing, you can get a fairly realistic idea of how visible your character is to the enemies.
Lastly, when you have all of that working, you should put the spotlight back on, in such a way that it always reflects the AI raycasting- so that the player always knows where the enemy is looking.