Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

how to make a system to check if i see certain object in unity

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class CameraFrustumTracking : MonoBehaviour
{
    public Camera displayCamera;
 
    public GameObject []Targets;
 
    // Start is called before the first frame update
    void Start()
    {
        if (displayCamera == null)
        {
            displayCamera = Camera.main;
        }
 
        Targets = GameObject.FindGameObjectsWithTag("Target");
    }
 
    // Update is called once per frame
    void Update()
    {
        foreach (GameObject target in Targets)
        {
 
            Plane[] planes = GeometryUtility.CalculateFrustumPlanes(displayCamera);
            if (GeometryUtility.TestPlanesAABB(planes, target.GetComponent<Collider>().bounds))
            {
                print("The object" + target.name + "has appeared");
                target.GetComponent<MeshRenderer>().enabled = true;
            }
            else
            {
                //print("The object" + target.name + "has disappeared");
                target.GetComponent<MeshRenderer>().enabled = false;
            }
 
        }
    }
}
 
PREVIOUS NEXT
Tagged: #system #check #object #unity
ADD COMMENT
Topic
Name
5+1 =