Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void CalculateLimits(Camera aCam, Bounds aArea, out Rect aLimits, out float aMaxHeight)
- {
- // Half the FOV angle in radians
- var angle = aCam.fieldOfView * Mathf.Deg2Rad * 0.5f;
- // half the size of the viewing frustum at a distance of "1" from the camera
- Vector2 tan = Vector2.one * Mathf.Tan(angle);
- tan.x *= aCam.aspect;
- // the center point of the area and it's ectents
- // the center point is taken from the bottom center of the bounding box
- Vector3 dim = aArea.extents;
- Vector3 center = aArea.center - new Vector3(0, aArea.extents.y, 0);
- // the maximum distance the camera can be above the area plane for each direction
- Vector2 maxDist = new Vector2(dim.x / tan.x, dim.z / tan.y);
- // actual distance of the camera above our plane
- float dist = aCam.transform.position.y - center.y;
- // the max movement range around the center of the plane
- dim.x *= 1f - dist / maxDist.x;
- dim.z *= 1f - dist / maxDist.y;
- // maxumum worldspace y coordinate the camera can be moved to
- aMaxHeight = center.y + Mathf.Min(maxDist.x, maxDist.y);
- // the min and max x and z coordinates the camera can be at the current distance.
- aLimits = new Rect(center.x - dim.x, center.z - dim.z, dim.x * 2, dim.z * 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement