- Home /
Check if object is being rendered
Is there a way to check if a object is within any cameras render area?
Answer by jtbentley · Sep 07, 2010 at 01:42 PM
Yes! I don't know how often you'd want to check it, but if you want it per frame..
var objectToStalk : GameObject; private var objectToStalkRenderer : Renderer; // Cache this so its faster
function Start() { objectToStalkRenderer = objectToStalk.renderer; // This reduces a get_component call in the update loop }
function Update() { if (objectToStalkRenderer.isVisible) print ("Jessica Alba is hot."); }
Answer by ThatOwlGuy · Jan 03, 2017 at 07:33 AM
Note that the object is considered visible when it needs to be rendered in the scene. For example, it might not actually be visible by any camera but still need to be rendered for shadows. When running in the editor, the scene view cameras will also cause this value to be true.
from --> https://docs.unity3d.com/ScriptReference/Renderer-isVisible.html
(Just in case anyone's still looking for this solution)
Answer by nathaxx · Jul 27, 2021 at 12:47 PM
sorry for wakening an old thread, but in case a newbie stumbles upon this:
Nowadays it should be called gameObject.GetComponent<Renderer>.isVisible
.
The GOAT. I could tell this was on old question by one of the comments referencing Jessica Alba.
Answer by cristian-chidean · Apr 21 at 02:53 PM
If you are using a Screen Space Canvas, you can check with this script:
using UnityEngine;
public class VisibleTest : MonoBehaviour
{
RectTransform rect;
Rect screenBounds = new Rect(0f, 0f, Screen.width, Screen.height);
void Start()
{
rect = GetComponent<RectTransform>();
}
void Update()
{
Debug.Log(IsVisible());
}
public bool IsVisible()
{
Vector3[] corners = new Vector3[4];
rect.GetWorldCorners(corners);
for (var i = 0; i < corners.Length; i++)
{
if (screenBounds.Contains(corners[i]))
{
return true;
}
}
return false;
}
}