- Home /
Detect if sprite is visible or not
I'm trying to figure out how to detect if a 2D sprite is visible from a specific camera, so there is a background sprite on which I am spawning (and growing) more sprites. I want to detect if the original background is still visible at any point or not. I was trying with
private var cam : Camera;
private var planes : Plane[];
function Start() {
cam = Camera.main;
planes = GeometryUtility.CalculateFrustumPlanes(cam);
}
function Update() {
if(GeometryUtility.TestPlanesAABB(planes,gameObject.collider2D.bounds))
Debug.Log(gameObject.name + " has been detected!");
else
Debug.Log("Nothing has been detected");
}
but unfortunately it doesn't detect if the sprite is covered with other sprites.
Thanks in advance.
Answer by Sisso · Jun 27, 2014 at 03:06 PM
Unity know when something is rendered, you simple need to find in the API how to access this information.
If the object is rendere to ANY camera is ok for you, simple use the Renderer.isVisible
I tried you method but unfortunately it doesn't seem to work, I only use the main camera and built it on my phone(so the editors camera cant cause problems), the Renderer.isVisible stays true even if the sprite is covered with other sprites.
Answer by unity_ZYfGXfm5YeGPkQ · Mar 18, 2019 at 07:22 PM
Hi ! I am facing the same issue.
I would like to detect if a sprite is visible to the player.
I tried OnBecameVisible /OnBecameInvisible / Renderer.isVisible but it also mark as visible the sprite that are idden under other layers.
If someone could help me, i would be very grateful. Thank you
Answer by sayginkarahan · Jan 20, 2021 at 10:03 PM
You can ray cast from the camera to your sprite position. And check the hit info.
True. If the coordinates in screen space fit within the range of your display (e.g. 1920 x 1080), it's on your screen.
Your answer
Follow this Question
Related Questions
Make a Sprite visible 1 Answer
Detect animation end when drag all sprite to Hierarchy view 1 Answer
Create an invisible area behind 2D objects. 2 Answers
Sprite not visible from behind 0 Answers
How to alternate images in 2d unity 0 Answers