Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

You can get events when an object is visible within a certain camera, and when it enters or leaves, using these functions:

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;
            }
 
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: declare multiple variables in for loop C# 
Csharp :: Get location in Xamarin - NAYCode.com 
Csharp :: serial begin 
Csharp :: unity stop object from rotating 
Csharp :: how to stream video from vlc in c# 
Csharp :: check if object has parent unity 
Csharp :: c# max sequence contains no elements 
Csharp :: c# array to label 
Csharp :: Get a list of distinct values in List 
Csharp :: input.getbutton unity 
Csharp :: HtmlToPdfConverter 
Csharp :: generate random light colors programatically in android 
Csharp :: run a command line from vb.net app 
Csharp :: Save variable unity 
Csharp :: trygetvalue c# 
Csharp :: c# int division to double 
Csharp :: Get replace normal text from word document in C# 
Csharp :: narcissistic 
Csharp :: reflection get enum value C# 
Csharp :: list view in unity 
Csharp :: datatable select c# 
Csharp :: raycasting unity 
Csharp :: unity trygetcomponent 
Csharp :: concurrent post request c# 
Csharp :: set time on audio source unity 
Csharp :: how to c# 
Csharp :: how to make font factory text to bold in c# 
Csharp :: MissingMethodException: PlayerManager.OnPlayerLeft Due to: Attempted to access a missing member. 
Csharp :: c# multipthreading 
Csharp :: how to extract data from a document using c# 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =