- Home /
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.
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?
Answer by Owen-Reynolds · Apr 08, 2013 at 03:00 AM
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.
Hey Owen, thanks for the reply. I won't lie, your comment confused me a bit and I wasn't sure what you were trying to say in the first few sentences. That said it did help push me in the right direction to solving my problem, especially the last two sentences of your answer. So with that I'm accepting the answer and adding how I came to a solution to my problem with what you said. Essentially, I modified the code that clamps the camera to rectangle bounds. This is the first part of that (transform.position in this case is the cameras position)
if (transform.position.y >= Boundaries.height + (maxOrthographicSize - camera.orthographicSize))
as you can see I add onto the boundaries height the maxOrthographicSize $$anonymous$$us the cameras current orthographic size. If the camera is at max size, there is no change. If it's not at max size however, it will adjust boundaries height accordingly.
The boundaries width was a bit more tricky but here is where you answer came into play. Once I realized that the orthographic size wasn't the same horizontally as it is vertically I started to try and figure out how to find the difference and then scale accordingly. It was pretty simple once I got there.
if (transform.position.x >= Boundaries.width + (((maxOrthographicSize / 10) 16) - ((camera.orthographicSize / 10) 16)))
Since I'm working with a 16:10 aspect ratio and from what you said about 50*AspectRatio I eventually came to this. Divide the maxOrthographicSize by 10 and then multiply it by 16, then do the same with the cameras current orthographic size. Camera works out well now!
For the 2nd line, could also use Camera.aspect
(which would be 16/10, I think.)
In the 1st part, don't change code that works. But, if you have similar problems:
You're still thinking the "largest camera" boundary is special. The math is really: if(trans.pos.y>= top(red)Edge - cam.othroSize)
. When the cam is at max size, that number will automatically work out to be your original Boundary height.
In other words, you did the math at first to get Boundary height. Can let the computer do it now.
Answer by shieldgenerator7 · Apr 16, 2020 at 10:50 PM
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.