- Home /
Is object at least partly visible?
Hi, I need to check if my camera is able to see a particular object. It doesn't need to see the whole object, it's enough if it's only partly visible. At the moment I'm using the IsVisibleFrom() Function. But this function only checks if the object is rendered. Sadly it's also rendered if another object obscures it completly. I think I will have to use Raycasts but my camera is not only a point, and that's why I can't find a solution.
Visibility is always complex stuff =D. Raycasts should work, but what do you mean that your camera is not a point? Its orthographic?
As an alternative, and somewhat costly way, you can try rendering a simplified world version in a secondary camera, invisible to the player. For example, walls are red and the object is white. If there is a single white pixel in the image generated by the secondary camera, the object is visible. Shaders experts might have a way to make this reasonably fast, I think.
Well.. the camera is a point but its view is not.
The way with the second camera sounds difficult. I prefer using Raycasts. Do you have any idea how to check this?-
As long the obstacles have colliders and/or rigid bodies, you can try raycasting from the camera position to a set of edge positions of the object you want to check its visibility. If one or more of the raycasts returns the object itself (or nothing, if the object does not have colliders) and no obstacles, then the object is visible. If your camera is not orthographic (or anything close to it), this approach should work. You might need to check distances because of the far and close planes of the camera.
So would you recommend to add a cuboid arround the object and check all 8 edges?
The simplest possible shape to represent your object. If you have to check against many objects at once, you might try to simplify to a rotated box and use, for example, 4 vertices of the box.
The problem with raycasting is a situation where, for example, all of the edges used to check visibility are behind a wall, but other parts of the object are actually visible. So choose the number of edges and structure of your scene in order to $$anonymous$$imize or avoid this problem.
Answer by robertbu · Feb 15, 2013 at 04:12 PM
You can cast a line from the eight corners of the renderer.bounds to the camera. This is not a perfect solutions since it is testing the bounding box rather than the mesh itself. I'm not sure how expensive it is, but here is a function:
bool IsBlocked()
{
Vector3 v3Corner = Vector3.zero;
Vector3 v3Center = renderer.bounds.center;
Vector3 v3Extents = renderer.bounds.extents;
v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top left corner
if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
return false;
v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z - v3Extents.z); // Front top right corner
if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
return false;
v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom left corner
if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
return false;
v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z - v3Extents.z); // Front bottom right corner
if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
return false;
v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top left corner
if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
return false;
v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y + v3Extents.y, v3Center.z + v3Extents.z); // Back top right corner
if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
return false;
v3Corner.Set(v3Center.x - v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom left corner
if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
return false;
v3Corner.Set(v3Center.x + v3Extents.x, v3Center.y - v3Extents.y, v3Center.z + v3Extents.z); // Back bottom right corner
if (!Physics.Linecast (v3Corner, Camera.main.transform.position))
return false;
return true;
}
Your answer
Follow this Question
Related Questions
Check if specific camera sees object 0 Answers
How to shoot a raycast with third person 1 Answer
Camera Zoom in&out 2 Answers
Activate object if visible and Deactivate object if invisible 2 Answers