Keeping a plain/terrain within the view frustrum

Hi all,
I’m making an RTS styled camera and I would like to make sure that the terrain is always covering the camera view.the Terrain is actually a plane and the camera is oriented looking down at the plane at 35deg or so.
I was thinking of testing the corners of the camera frusturm and if all 4 corners of the camera view frustrum hits the plane then it means the plane covers the camera view.
Can anyone offer code snippets? I am hoping to do it without using the camera position to check against some kind of imaginary bounds.

Thanks

Kev

You can get rays from the camera at a given spot, so one approach is to cast rays from the camera corners and make sure they hit your terrain, or hit your terrain bounding box.

To ignore screen pixel dimensions and just get the corner rays, this is the API you want:

The nice part is that it doesn’t matter how your camera is set, how your field of view is set, etc., It just works.

You can use this to raycast and hit physics on your ground using:

  • asks “did I hit any collider in the scene?”

If you are truly only interested in hitting one collider and one collider alone, colliders actually have their own Raycast, that only checks them and nobody else:

  • asks “did I hit this particular collider?”

Or alternately, if you prefer to hit a mathematical Plane (completely different from the “Plane” primitive object!), you can use:

  • asks “did I hit this plane?”

When using any of these methods, it is very common to have a proxy GameObject (or GameObjects) to mark a movement boundary slightly smaller than your actual terrain, and to use that smaller piece as the limit. This gives you better control, divorcing presentation from logical checking of camera movement.

Thanks for the reply and the help. Appreciate it.
Just in time too as I was just about to start working on the camera this week.

1 Like