Check to see if my array of gameobjects is visible to the camera

Im creating a mobile game using the gyroscope to control with. The idea is that obstacles is coming from the right side going towards the left. All around you are spawning obstacles, even outside of the camera to ensure that the gyroscope actually has a function and you can go in any direction you want.

So i have trouble figuring out how to get check whether my gameobjects in a array is visible to the camera or not - and based on that i want to disable the renderer on those which are not.

public class CameraTest : MonoBehaviour {

    public List <Vector3> obsty_position;
    public List <GameObject> obsty;

    private Camera cam;
    // Use this for initialization
    void Start () {

   
     
    }
 
    // Update is called once per frame
    void Update () {

        obsty = new List<GameObject>();
        obsty.AddRange(GameObject.FindGameObjectsWithTag("Obstacles"));
        Debug.Log("Obsty :" + obsty.Count);
        EnemyArray();
     
    }
    void EnemyArray ()
    {
        foreach (GameObject g in obsty)
        {

            if(g.renderer.isVisible == false)
            {
                g.renderer.disable;
            }
         
         
        }
     
    }

The foreach loop is my idea on how to do it - but it does not work at all.
Hope you guys can figure it out, help me out or point me in the right direction :slight_smile:
Kind Regards,
Lars.

CullingGroup might be useful in this case. Check the manual on how to use it.
After applying it, you can use BoundingSphere to detect if the sphere (object) is visible.

Or, you could calculate Vector3.Dot() to check if the object is in front of the cameraTrm.forward.
Although, it would require calculating directions & dots for each obstacle.

1 Like

Hey! thanks for the reply. I did some investigation and combined some CullingGroup and ObjectPooling and got some good optimization! thanks again!