Hi, this is my first attempt at Unity and I ran into a problem. I need to detect whether the player character is visible from a point (think CCTV camera).
The first idea is, like many and many suggest, to simply raycast and check the collision. Quick, easy, but it seems wrong. Imagine a situation when the player is behind a wall with a small window that allows the camera to only see his knees (for example). In this situation raycasting to the center of the model, the feet, head or any other arbitrary point will fail. Yet the CCTV can see the player.
I thought about CapsuleCast, but that will not work either, as the sweep will stop once it hits the wall, it will not report any further collisions.
Raycasting to all the vertices of the character mesh sounds pretty expensive. Is there an efficient and correct way to check for visibility?
Thanks!
Fake it with triggers. It’ll get the job done and is easy.
If not, look for 3rd party solutions on the asset store etc.
If not, be prepared to write code that bakes view volumes from the camera. It could be done as part of your level loading or as part of your build process. The view volumes could be composite convex mesh collider triggers. As for correctness, that is a touchy subject. If there was a 1 mm gap somewhere in a wall, should the camera see it? I mean, to be correct, it should. But practically, should it matter? No, I don’t think so at least.
As a final option you could consider rendering the scene to a texture, where everything that isn’t detectable is black and everything that is detectable is white. Then you can have multiple shader passes which spits out the max value of texel of two comparisons. If so, the camera has detected something, but it can’t identify which object. You could be clever and support identification of 4 different objects by linking them to red, green, blue and alpha channels respectively. If you just want to make a camera like those automatic doors at malls that open the door or raise the alarm, it would do just fine and should be pretty efficient and have decent resolution and correctness, and would work for cameras that move around. If you are confused about this approach I could invest some time in trying to make some pictures to explain the concept.