I’ve spent several hours now trying to work out which GameObjects a player can “see” within the field of view of the First-Person Camera. I’ve come up with a script to attach to every GameObject that calculates the angle between the player and the object and determines if the object is within the field of view, however I want to follow this up with a raycast to determine if the player’s view is obstructed.
The problem I’ve run into is that the raycast is performed to the center of the GameObject, which, depending on the position of the player, can be obstructed by nearby objects even if it’s possible to see the object through the camera. I assume the solution would be to raycast from the surface of the object rather than the center, the problem being that I would need to cast from every part of the surface, which is obviously impractical.
Am I missing an obvious solution to this problem, or even a better method of obtaining a list of GameObjects currently in view of the camera that aren’t fully obstructed by another GameObject.
Screenshot attached to illustrate the problem (there also appears to be an issue with my field of view calculation, but I’ll deal with that later. The green debug ray shows the raycast is being performed).
Edit:
I should also mention this is Unity 2.5, so any fancy U3 solutions won’t help I’m afraid 
You can cast rays from the camera with Camera.main.ScreenPointToRay(Vector3) however this would require you to cast a ray from every pixel which, I would guess, would be quite recourse hungry.
Sorry I cant be more help, just started learning Unity yesterday.
There is probably a more efficient way, but you can use a simplified version of Monte Carlo technique:
Find the visual angle ( Angular diameter - Wikipedia) with atan or using the faster small-angle approximation (Small-angle approximation - Wikipedia).
fire multiple rays from the player’s eye to the (object center ± half the visual angle)
You can probably fire one ray per update(), but you may need more.
note this is off the top of my head, so math may be a bit wrong.
@ifandbut Given it a try and raycast every pixel in the Camera’s view, as expected the framerate is awful 
@pakfront I think I managed to implement what you suggested, however I originally did this for the x-axis, expanding to the y-axis seems to cause too much slowdown.
Any more suggestions? It drives me crazy that I can see the objects on the screen but unity wont tell me what they are 
How accurate does it need to be? Maybe casting to center + each corner of the bounding box and if any are visible consider the whole thing visible?
@niosop Tried this but no luck with spheres.
For my method, the random variation on the x-axis only may be sufficient unless you have a lot of low walls that are lower than the character max height but higher than the center.
Thinking about it, there is no need for the expensive angle conversion - just choose a To point that is randomly placed inside the enemy box collider. YOu could maybe do 2 - one randomly above center or one randomly below.
Another way is to use multiple colliders for the enemies - a box for forearm, upper arm, torso, etc. Then test against all of these.
Anyway, I’m sure this is a common problem, maybe some creative googling is the key?