Hello, I’ve been trying to make a script to disable objects that are not in view of the main camera, this is what I have so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Map : MonoBehaviour {
public GameObject[] gameObjects;
public float x;
public float y;
private void Update()
{
UpdateObjects(gameObjects);
}
void UpdateObjects(GameObject[] objects)
{
foreach(GameObject g in objects)
{
x = Camera.main.WorldToScreenPoint(g.transform.position).x;
y = Camera.main.WorldToScreenPoint(g.transform.position).y;
if (x < 0 || x > Screen.width)
{
g.SetActive(false);
}
else if (y < 0 || y > Screen.height)
{
g.SetActive(false);
}
else
{
g.SetActive(true);
}
}
}
}
This partially works, however, objects behind (behind being a cast of FOV of the camera backwards) the camera still are enabled, thanks for any responses!
example images: Imgur: The magic of the Internet