Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Question Keeping a plain/terrain within the view frustrum

Discussion in 'Scripting' started by kyapp69, Jul 22, 2020.

  1. kyapp69


    May 12, 2020
    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.


  2. Kurt-Dekker


    Mar 16, 2013
    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.
    Last edited: Feb 15, 2021
  3. kyapp69


    May 12, 2020

    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.
    Kurt-Dekker likes this.