Need help with 2D orthographic camera zooming

I’ll do my best to explain my problem!

Black dot = The cameras position.

Black Rectangle Outline = Camera boundaries, the camera is confined inside of this rectangle.

Blue Rectangle = Cameras vision, what the camera can see based on the orthographic size

Red Rectangle Outline = The maximum of what the camera can see when the camera is fully zoomed out based on my orthographic size clamping setting.

Green Rectangle Outline = The extent of which the camera can see when the camera is fully
zoomed in, again based on my orthographic size clamping setting.

9811-example.png

As you can see in figure (b), when the camera is zoomed in the camera cannot move any lower and see what’s under its view rectangle because it’s still confined inside the black outlines rectangle/boundary. Essentially what I’m trying to figure out, how can I increase the boundaries scale as I decrease the cameras orthographic size?

Suppose the right side is x=100 and your camera 1/2-width is 20. That means your black line is at x=80. But, the math is 100-camWidth. When viewWidth is 10, the limit is 90. Just replace your numbers with RedLimit-CamWidth.

Then there’s an extra multiplier for width(?) for the Aspect Ratio. I think a camera size 50 means 50 in the shorter direction, and 50*AspectRation in the longer.

This solution only requires you to define the red rectangle

Bounds visibleBounds = new Bounds(...);//bounds of red rectangle, in world coordinates

//Convert screen sizes to world coordinates
Vector2 cameraSizeWorld =
    Camera.main.ViewportToWorldPoint(Vector2.one)
    - Camera.main.ViewportToWorldPoint(Vector2.zero);
Vector2 halfSize = cameraSizeWorld / 2;

//Use camera size to keep it in the bounds
Vector3 pos = Camera.main.transform.position;
pos.x = Mathf.Clamp(
    pos.x,
    visibleBounds.min.x + halfSize.x,
    visibleBounds.max.x - halfSize.x
    );
pos.y = Mathf.Clamp(
    pos.y,
    visibleBounds.min.y + halfSize.y,
    visibleBounds.max.y - halfSize.y
    );
Camera.main.transform.position = pos;

And then your camera will stay within the bounds of the red rectangle, even if you change its orthographicSize property.