- Home /
How to tell if an object that behind another object can be seen?
Hello, I am trying to find out how to check if an object that behind another object can be seen by camera. I figured out a way of doing it but it requires 32400 rays to be shoot out from the camera per frame which i think that is a lot of process to do. So what would be the easiest way to achieve this? What i've been thinking about for now is checking which object is behind another and then comparing the bounds of colliders of these two object and see if front object has a bigger collider so the object behind cannot be seen by the camera BUT this won't work if it is a complex geometry..Any ideas? Thank you in advance!
Can you fudge it? If so, you can use the object's renderer's bounds field (http://docs.unity3d.com/ScriptReference/Renderer-bounds.html) to get the 8 corners of the cube that contains the object. If a raycast to any corner does not hit another object, the object is NOT completely obscured, just partially obscured. If all 8 hit another object, it IS obscured. If all 8 dont hit another object, it is completely visible.
This is just a FUDGE tho; consider a sphere bounded by a cube, lots of empty space between the cube corners and the sphere surface, could lead to incorrect results.
Another, more exact method, I don't think you can do without unity pro. It involves rendering your scene to a texture rather than the screen. With this method you can render all your objects (without lighting) as say.. black.. except the object your want to detect- that one you draw white. Then, check the rendered texture for any white pixels. This method would require you create special shaders for rendering the texture. Edit: you can even go crazy with this method and have each object drawn and indexed by it's own "color", rather than simply white or black. This is how I USED to do mouse over detection (pre-unity).
I was hoping to re-create unity pro's feature of "Occlusion Culling". I can get accurate data with raycast but as i said it requires maximum 32400 rays which is pretty heavy i think. What i did was shooting a ray from the camera at al angles one by one and return the result of it, this works but slowly. Your method is crazy and i am just a beginner! I thought the collider's bounding box but that wouldn't be accurate so i passed that option. Still trying to get around on this. Thanks for the tips!
I believe that occulision culling is in the free version of Unity 5 too.
32400 rays
Woah! Even if you cast a ray to every single vertex of the target how do you get 32$$anonymous$$? (32$$anonymous$$ vertex mesh? I hope not.)
Answer by KdRWaylander · Apr 10, 2015 at 07:05 AM
Hi,
You can use the built-in function which is renderer.isVisible
: http://docs.unity3d.com/ScriptReference/Renderer-isVisible.html
Hello, i will look for this and see if this was that i was looking for.
Your answer

Follow this Question
Related Questions
Internal collisions 1 Answer
Using a Single Collider to Detect Collisions with Multiple Colliders 3 Answers
OnTriggerEnter not being called with multiple colliders 1 Answer
Photon - Add force to other player 0 Answers
Attempt counter 2 Answers