Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by feneq · Apr 07, 2013 at 11:11 PM · camera2dscalezoomboundary

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.

alt text

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?

example.png (5.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image feneq · Apr 08, 2013 at 06:17 AM 0
Share

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!

avatar image Owen-Reynolds · Apr 08, 2013 at 02:00 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change UI scale in relation to 3D camera 0 Answers

Implementing Camera bounds proportionally to its ortographic size 0 Answers

Is there a way to zoom GUI and scene at the same time? 0 Answers

How to scale gameobject width with camera? 0 Answers

2D camera zoom smoothing and limitations? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges