- Home /
Line of Sight - how to deal with partially visible objects?
I'm working on a mechanic that causes objects to interact when they're seen by the player's first person camera.
So far, I'm using a "LookReaction" script that goes on any object that will react to being looked at. It uses GeometryUtility.CalculateFrustumPlanes and GeometryUtility.TestPlanesAABB to check if the object intersects with the camera frustum. If it does, it uses Physics.Linecast from the camera to the object's position to tell if there's another collider in the way. If the Linecast returns the desired object's collider, the object counts as being looked at.
It works great, except for objects that are partially occluded by other objects. Since the line is cast to the center of the object, it doesn't count as being in line of sight if less than half is poking out from behind another object.
What's a good solution for catching objects that are mostly blocked from view? Thanks for any help!
Answer by FortisVenaliter · Feb 03, 2017 at 10:03 PM
Why don't you have it test with a ray from the center of the screen rather than to the center of the object? That way it will trigger if the center of the screen is looking at the object and not blocked.
Unfortunately this wouldn't work because I need the object to count as seen even if it's not centered on the screen.
That's more difficult. You could try having multiple raycasts to various points, but that would be processor intensive. You could also render to a low-res alternate version of the screen where non-selectable objects are flat black and selectable are brightly colored. Then turn off anti-aliasing and check the produced image for the color of the object in question to deter$$anonymous$$e if it's on-screen.
both of these are good ideas.
i wouldn't worry too much about the performance hit of multiple raycasts. if you're concerned, a simple test would be to take your current code and just repeat the raycast 100x or whatever and see how it goes. i would guess that 5 or 9 or so raycasts would be sufficient, but it all depends on the situation.
for the image approach, i think you'd set up a second camera and have it render to a texture. look up 'rendertexture'.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110249im_/https://answers.unity.com/themes/thub/images/avi.jpg)