Hi,
In FirstPersonCamera I need my character to know what are the objects that he sees from his attached camera. As he moves, the list of seenObjects should be updated. Giving the fact that all the objects are tagged with unique names.
I want my character to know the object and its distance.
Regards,
Ibrahim
One solution could be to use Physics.OverlapSphere and get all the colliders which touches or is inside the sphere. The center would be the camera, and the radius of your choice. You can even set a layerMask, to get colliders on a particular layer. (Filter out the objects that is not in the direction of the camera.) Then find the distance between the camera and the collider object with Vector3.Distance.
There are a number of ways of solving the problem of knowing if an object is visible from a camera. The first step is getting the list of potential game object. If there is a distance limit, you can use Physics.OverlapSpere() as @bompi88 suggests. Or you can tag them all and use GameObject.FindGameObjectsWithTag() or you can maintain your own list in a Singleton, or… Once you have the list, here is a link to information about how to check if a specific object is seen. If you google “how to tell if an object is seen unity3d,” you will find lots of hits and advice, but this is the most complete answer I know about:
http://answers.unity3d.com/questions/8003/how-can-i-know-if-a-gameobject-is-seen-by-a-partic.html
Note that if you decide to use the Renderer.isVisible flag, that it will return true if the camera in the Scene view in the editor sees the object. So your results running this code might not work correctly when your app is run in the editor.
Note if you don’t want your character to see through items, the problem get some harder. There are also a number of posts on this problem as well.