This one’s beyond me. I could do physical triggers, that is a feasible alternative, but it would interest me to know how to create a boolean that is determined by whether or not it’s a given GO is in the camera’s view frustum. Useful for a heat seeking missile, as heat seekers “give up” if it’s target has been lost.
In addition there is also the possibility to project their position with the cameras WorldToScreenPoint function and look if the point is inside the 0,0,Screen.width,Screen.height rect (for example if you need that for click interaction or an overlay info screen anyway)
Gotta love quick replies
Now isVisible is basically if the object is visible in any camera. Can I get it to decide if it is visible in just it’s own camera?
To check against a specific camera, I’d use dreamora’s suggestion of WorldToScreenPoint.
Check if the resulting X and Y coordinates are between 0 and screenPos.x and screenPos.y and if they are, the target is ‘on screen’ for that camera.
Would do, but I devised a much better system for making my missiles lock on.
Just a quick note on this:
-
The renderer flags things as “visible” if they’re casting dynamic shadows regardless of if they’re actually on-screen, which means you can’t use this check. I assume this is because it has to render them if they’re within dynamic shadow range in case they’re casting shadows on something that is actually visible. This doesn’t seem very efficient, as it means you could have a load of meshes being animated and rendered even if they’re behind the camera or a wall and not casting shadows on anything that’s visible.
-
WorldToScreenPoint will return the same value if you’re facing directly away from the object as it does if you’re facing it, so it can’t be used for a visibility check unless I’m doing something wrong.
-
You really don’t need this (or cameras) to check if a missile can “see” something. You could instead get the dot product of the missile’s forward direction against the vector to it’s target. That would tell you if the target was in front of or behind the missile.
If you’re using dynamic shadows and want to check if something is visible, you could use something like this:
public bool IsVisibleFrom(Renderer renderer, Camera camera)
{
Plane[] planes = GeometryUtility.CalculateFrustumPlanes(camera);
return GeometryUtility.TestPlanesAABB(planes, renderer.bounds);
}
This will return true if the renderer is within the camera’s view frustrum. You would then have to raycast or something to check for occlusion. This seems like a terrible hack, so please correct me if there’s a better (tested) way of checking object visibility when that object is casting dynamic shadows.
The Z element is positive if the object is in front of you, and negative if it’s behind you. So the value isn’t the same, and indeed it works well as a visibility check (I’ve used this).
–Eric
Ha, thanks for that, I didn’t even look at what it was returning in Z. That should work fine then, although checking the bounds of the renderer against the view frustrum might be more useful for larger objects, unless you just need to know if the object’s center is within the screen.
That’s big brain move, gotta test this now! <3 <3