• 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
5
Question by alexanderflink · Jul 31, 2014 at 07:54 AM · camera2dscaleorthographicwidth

Resizing orthographic camera to fit 2d sprite on screen

Hello!

I have a game object made up of multiple sprite game objects. Each sprite is a "block" (think terraria / minecraft) and the main gameobject is a collection of these blocks. I need my orthographic camera (it's a 2d game) to shrink it's width according to the width of the game object. Basically, I need to do the opposite of what orthographic.size does. The height of the orthographic camera doesn't matter, but the width does. The main game object that consists of the "ground blocks" must always fill the screen width, the height can then stretch as far as it needs for it to be proportional. I've searched for something like "orthographic width", but found nothing. How would I go about doing this?

Hope my explanation isn't too muddy.

Also, if you have a suggestion on how to better solve this problem than resizing the width of the camera according to the main game object, please say so.

Thanks!

P.S Basically, I understand what I probably need to do: calculate the orthographic size depending on the width of the game object that I want to fit on the screen. However, I can't figure out the maths to do this.

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

1 Reply

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

Answer by robertbu · Jul 31, 2014 at 01:56 PM

Orthographic size is 1/2 of he vertical size seen by the camera. So try this:

  Camera.main.orthograpthicSize = widthToBeSeen * Screen.height / Screen.width * 0.5;
Comment
Add comment · Show 8 · 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 alexanderflink · Jul 31, 2014 at 02:05 PM 0
Share

Works like a charm, thank you!

avatar image Vipul-Dudharejiya · Jan 31, 2015 at 06:23 AM 1
Share

what is widthToBeSeen?

avatar image jjxtra · May 15, 2015 at 05:44 PM 0
Share

widthToBeSeen is the width of the sprite I think

avatar image vml933 · Aug 14, 2015 at 09:44 AM 0
Share

Thanks,solve my problem too,it suffered me a long time!

avatar image FlightOfOne · Jan 04, 2018 at 04:59 PM 0
Share

be sure to convert to float, or you may get 0.

((float)Screen.height / (float)Screen.width) * 0.5f

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

26 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 avatar image avatar image 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

Sprite relative position/scale problem with orthographic camera 0 Answers

Why is part of object outside camera frustum visible? 1 Answer

Making a camera semi-orthographic 2 Answers

2d orthographic camera follow 1 Answer

ScreenToWorldPoint not accurate 0 Answers


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