Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by DragonBorn007 · Aug 30, 2013 at 04:39 AM · cameraorthographicaspect ratio

Changing Ortho Cam Size according to resolution

Ok this may be an easy question to answer, but I am terrible at math and need some help at this. So I am creating a 2d space shooter game in which the enemies and the player cannot go past the screen. The boundaries depend on the game's current resolution, so if I change the resolution the player and the enemies may be able to go further accross the screen or may go less depending on the resolution setting. I have figured that changing the orthographic camera size according to the screen's resolution would fix the problem, but I am having trouble getting the code down since there is a basic level of math that I need more understanding in. I.e simple division between aspect ratios.. So if anyone could help with the code and perhaps add a link to some math tutorial vids/site/etc that would help in solving the problem, I would very much appreciate it :)

Comment
Add comment · Show 1
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 daniloulf · Jan 06, 2018 at 01:47 PM 0
Share

how it works for perspective camera?

4 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by luislodosm · Mar 16, 2017 at 11:38 PM

 [ExecuteInEditMode]
 public class CameraManager : MonoBehaviour 
 {
     public float horizontalResolution = 1920;
 
     void OnGUI ()
     {
         float currentAspect = (float) Screen.width / (float) Screen.height;
         Camera.main.orthographicSize = horizontalResolution / currentAspect / 200;
     }
 }
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
avatar image
2

Answer by s_guy · Aug 30, 2013 at 04:48 AM

Here's a decent overview that discusses pixel perfectness (no artifacts messing with your source art), how to manage the size of art assets on screen, and formula for determining orthographic camera size.

ortho cam tutorial

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

Answer by magician_creator · Apr 15, 2020 at 07:25 PM

I hope that this will help someone.. this script will change the camera (orthographic) size depending on the resolution. Add this script to the main camera.

 bool MaintainWidth = true;
 Vector3 CameraPos;
 float DefaultWidth;
 float DefaultHeight;

 void Start()
 {

     CameraPos = Camera.main.transform.position;
     
     DefaultWidth = Camera.main.orthographicSize *  the resolution the game was designed for;
     DefaultHeight = Camera.main.orthographicSize;

     if (MaintainWidth)
     {
         Camera.main.orthographicSize = DefaultWidth / Camera.main.aspect;
     }

     Camera.main.transform.position = new Vector3(CameraPos.x, -1 * (DefaultHeight - Camera.main.orthographicSize), CameraPos.z);

 }





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 okan92topal · Apr 30, 2020 at 10:53 AM 0
Share

what about if camera follows our player :) ?

avatar image magician_creator · May 01, 2020 at 10:29 AM 0
Share

@okan92topal

Just make your camera a child of an empty gameobject and add the camera's following script you have made to the empty gameobject. The code i have given above will still be added to the main camera.

avatar image
0

Answer by Crayden · Jan 23, 2021 at 01:52 PM

I wanted a reusable solution. Where you can just fill in the screen aspect ratio that you designed on and the starting size of your orthographic camera.

The GameObject the camera is focused on will always respect the minimum spacing to either the top or the side of the screen, depending on the current aspect ratio.


 private void OnGUI()
     {
         float aspectRatioDesign = (16f / 9f);
         float orthographicStartSize = 3.8f;
         
         float inverseAspectRatio = 1 / aspectRatioDesign;
         float currentAspectRatio = (float)Screen.width / (float)Screen.height;
 
         if (currentAspectRatio > aspectRatioDesign)
         {
             currentAspectRatio -= (currentAspectRatio - aspectRatioDesign);
         } else if (currentAspectRatio < inverseAspectRatio)
         {
             currentAspectRatio += (currentAspectRatio - inverseAspectRatio);
         }
 
         Camera.main.orthographicSize = aspectRatioDesign * (orthographicStartSize / currentAspectRatio);
     }
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

21 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

Related Questions

OrthographicSize on Android changes from Start to Update? 0 Answers

Change Editor Camera Orthographic Size 2 Answers

2D Aspect Ratio Of Camera 2 Answers

Resizing orthographic camera to fit 2d sprite on screen 1 Answer

Orthographic Camera Size? 3 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