Advertisement
Bunny83

CalculateCameraLimits

Mar 26th, 2018
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1.     public static void CalculateLimits(Camera aCam, Bounds aArea, out Rect aLimits, out float aMaxHeight)
  2.     {
  3.         // Half the FOV angle in radians
  4.         var angle = aCam.fieldOfView * Mathf.Deg2Rad * 0.5f;
  5.  
  6.         // half the size of the viewing frustum at a distance of "1" from the camera
  7.         Vector2 tan = Vector2.one * Mathf.Tan(angle);
  8.         tan.x *= aCam.aspect;
  9.  
  10.         // the center point of the area and it's ectents
  11.         // the center point is taken from the bottom center of the bounding box
  12.         Vector3 dim = aArea.extents;
  13.         Vector3 center = aArea.center - new Vector3(0, aArea.extents.y, 0);
  14.  
  15.         // the maximum distance the camera can be above the area plane for each direction
  16.         Vector2 maxDist = new Vector2(dim.x / tan.x, dim.z / tan.y);
  17.  
  18.         // actual distance of the camera above our plane
  19.         float dist = aCam.transform.position.y - center.y;
  20.  
  21.         // the max movement range around the center of the plane
  22.         dim.x *= 1f - dist / maxDist.x;
  23.         dim.z *= 1f - dist / maxDist.y;
  24.  
  25.         // maxumum worldspace y coordinate the camera can be moved to
  26.         aMaxHeight = center.y + Mathf.Min(maxDist.x, maxDist.y);
  27.  
  28.         // the min and max x and z coordinates the camera can be at the current distance.
  29.         aLimits = new Rect(center.x - dim.x, center.z - dim.z, dim.x * 2, dim.z * 2);
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement