[SOLVED] Check if gameobject without renderer is within camera view

I have a camera that looks down towards the player.

I have alot of Vector3 positions that is placeholders for pooled gameobjects.

When 1 or more of these positions are within a certain distance from the camera view I want to activate the pooled gameobjects and move them to those locations so they will be visible to the player.

I know how to activate and deactivate and all that but I cant seem to figure out how to make the camera know when the positions are in view.

I can not use OnBecameVisible or OnBecameInvisible as I only have locations and no renderer at the locations until the pooled objects are moved to those locations.

Do anyone know how to do this or any ideas that may help?

You can test if the object is within the camera frustum:

using UnityEngine;

public class Example
{
    Transform t;

    Camera cam;
    Plane[] planes;

    void Start()
    {
        cam = Camera.main;
    }

    void Update()
    {
        // Move this line to `Start` if the camera does not move / rotate
        planes = GeometryUtility.CalculateFrustumPlanes(cam);
        if (GeometryUtility.TestPlanesAABB(planes, new Bounds(t.position, Vector3.zero))
        {
            Debug.Log(t.name + " has been detected!");
        }
        else
        {
            Debug.Log("Nothing has been detected");
        }
    }
}

Source: Unity - Scripting API: GeometryUtility.TestPlanesAABB

[SOLVED] I got this to work :slight_smile:

In Update:

if(IsVisible(transform.position, transform.GetComponent<Collider>().bounds.size, Global.MainCamera)) {
                for(int i = 0; i < GlobalPoolingGrass.GrassList.Count; i++) {
                    if(!GlobalPoolingGrass.GrassList*.activeInHierarchy) {*

if(i == m_grassPosition.Count) {
break;
} else {
GlobalPoolingGrass.GrassList.transform.position = m_grassPosition*;
GlobalPoolingGrass.GrassList.transform.rotation = new Quaternion(0f, m_grassRotation, 0f, 0f);
_GlobalPoolingGrass.GrassList.SetActive(true);
break;
}
}
}
}
And the Function “IsVisible” Not created by me
bool IsVisible(Vector3 pos, Vector3 boundSize, Camera camera) {
var bounds = new Bounds(pos, boundSize);
var planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, bounds);
}*_