How to detecting visible of a gameobject on/off?

Hello all,

I’m newbie and trying to test small game. My problem now: How to detect a gameobject “see” by main camera. Then add a alarm (popup/button …) to it. My scene have only 2 gameobject and maincamera. When i rotate it, same time a gameobject will behind another, so if it’s visible: add alarm for it.

I tested with renderer.isvisible but dont. Hopeffuly will have any help.

Thanks. Quen.

3 Answers

3

Renderer.isVisible is true or false based on whether or not the object needs to be rendered, which is not always the same as the object being in view, i.e. contributing pixels to the screen. In your case, it sounds like you’d like to detect whether an object is occluded by another, which is not the same condition as the one that sets renderer.isVisible.

Unity does include occlusion culling algorithms, but unfortunately, those are a Unity Pro feature and I’m not sure the API offers access to the results of its calculations, that is, whether it gives you callbacks that occur when objects become occluded. So you need to come up with something else.

A simple, and relatively easy approach to develop is to calculate the screen-space bounding box of your objects. If the screen-space bounding box of one object completely envelops the other (you’d use Rect.Contains), then test their depths. If the enveloped object has a greater depth than the enveloping object, it is likely occluded. This approach can fail for a number of reasons, particularly if the objects can intersect, but it might work in your situation.

yes, very useful, very detail, MANY Thanks and feedback for this way.

THIS may help you…Dharmesh

No, that doesn't tackle the OP's question at all. It discusses the merits and faults of isVisible just like my reply, and does not provide a solution for manually detecting occlusion. The top reply even mentions this himself: > "But if you must know whether the > object is obscured, things get way > more complicated - actually, there's > no reasonable way to check if any > pixel of the object is visible (maybe > analyzing the depth buffer with some > specialized shader)."

Now i have 2 way for test. :) First i will practice with occlude culling. Then is screen-space bounding box. Do you have any suggest or guide document for the screen-space bounding box, CHPedersen? Thanks

Ray Casts are quite popular for checking visibility through hit tests

No, Louis, I want detect visible of a gameObject and add a popup when it visible (appear). I'm not detect hit objects. Thanks

What Louis tries to say is, you could use a Raycast from the camera to the object to see if it hits the object meaning it is visible (there is nothing between the camera and the object. Anyway, CHPedersen's answer seems much efficient and secure, go for that.

Yah, test ok with Occlusion Culling. exactly what i want. So now i want add popUp/Button when gameObject appear, this popup much be move follow the object if rotate group. How can I do? Thanks.[32640-screen+shot+2014-09-19+at+10.29.35+pm.png|32640]

P/S. this solution just good for Sphere object. With another shape, i think not appropriate.