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 Diori · Aug 26, 2014 at 05:12 AM · camerascreenviewaspect ratio

Fixed width, relative height, on different aspect ratio screen?

I am developing a game in portrait view. I encounter a problem when the game running on different aspect ratio. On taller screen the camera clip the horizontal view. I am expecting a fixed horizontal view, and the clip should be on the vertical view. How can I achieve this on perspective camera and ortographic camera?

I think I can calculate the ortographic size to fit the width, and there is some answer about it. But I think the perspective size cannot easily calculated because of frustum and field of view.

alt text

question.png (6.7 kB)
Comment
Add comment · Show 2
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 LijuDeveloper · Aug 26, 2014 at 05:31 AM 0
Share

Try this

// Switch to 600 x 1024 fullscreen

     void Awake() 
     {
       Screen.SetResolution (600,1024, true); // adjust true or false as you want
     }

reference Screen.SetResolution

avatar image Diori LijuDeveloper · Aug 26, 2014 at 06:32 AM 0
Share

I tried this and it's not resulting as I expected. $$anonymous$$y objects get stretched. I want the objects stay in proportion, and the view of my camera adjust by cropping the height.

4 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by Cthaeh23 · Mar 28, 2017 at 03:14 PM

Here my solution for perspective cameras:

 //Set a fixed horizontal FOV
 private float horizontalFOV = 120f;

 //somewhere in update if screen is resizable
 cam.fieldOfView = calcVertivalFOV(horizontalFOV, Camera.main.aspect);
 
 private float calcVertivalFOV(float hFOVInDeg, float aspectRatio)
     {
         float hFOVInRads = hFOVInDeg * Mathf.Deg2Rad;  
         float vFOVInRads = 2 * Mathf.Atan(Mathf.Tan(hFOVInRads / 2) / aspectRatio);
         float vFOV = vFOVInRads * Mathf.Rad2Deg;
         return vFOV;
     }

Some explanation:

In Unity you can only influence the aspect and the vertical FOV.

From a given vertical FOV and the aspect ratio Unity calculates the horizontal FOV. So depending on the aspect ratio the horizontal FOV will change.

Now to get a fixed horizontal FOV I calculate the vertical FOV to the current aspect ratio and apply it to the camera. This way the horizontal FOV is fixed and the vertical will change.

Comment
Add comment · Show 5 · 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 Cthaeh23 · Mar 28, 2017 at 03:00 PM 1
Share

Credits to: https://gist.github.com/coastwise/5951291 $$anonymous$$y answer is just the inverse function.

avatar image atreyu64 · Oct 03, 2017 at 10:51 PM -1
Share

Thank you so much, it works great, exactly what I was looking for !

avatar image DevDop · Apr 18, 2018 at 04:50 PM 0
Share

Thank you so much, it works perfectly!

avatar image DevDop · Apr 18, 2018 at 04:51 PM 0
Share

Just in case someone sees this, with the last version of Unity you need to replace "cam." with "GetComponent()." ;)

avatar image davitsedrakian · Nov 14, 2019 at 05:03 PM 0
Share

that was great solution, and thank you for snippet and detailed explaination

avatar image
3

Answer by HungryGames · Dec 12, 2014 at 03:52 AM

camera.orthographicSize = camera.orthographicSize * defaultAspect / camera.aspect;

Comment
Add comment · Show 3 · 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 PouleyKetchoup · Feb 10, 2017 at 01:26 PM 1
Share

It worked great for me in the case of an orthographic camera!

I've implemented it this way in order to switch between respecting width or height depending on the aspect ratio:

 // initialSize is the default Camera.main.orthographicSize
 // targetAspect is the desired aspect ratio
 if (Camera.main.aspect < 1.6f)
 {
     // respect width (modify default behavior)
     Camera.main.orthographicSize = initialSize * (targetAspect / Camera.main.aspect);
 }
 else
 {
     // respect height (change back to default behavior)
     Camera.main.orthographicSize = initialSize;
 }
avatar image LilGames PouleyKetchoup · Jan 03 at 09:31 PM 0
Share

I'm loving this answer, but having trouble adapting it so the Horizontal FOV changes to always keep the WIDTH.

avatar image yong2khoo · May 02, 2021 at 10:26 AM 0
Share

thanks for your answer. i finally see some light in 2021. Portrait game supposed will encounter issue for multiple resolution. without compromising with the use of UI elements, guess changing orthographicSize as you mentioned, is the only way.

avatar image
3

Answer by adrielkon · Jun 26, 2017 at 09:41 AM

There's 2 way you can solve this problem:

By Changing the Camera FOV

It is the easiest solution, what you need to do is to set the Horizontal Camera FOV to The expected FOV or what i prefer "The design FOV"

Ex :

 var designAspect = 2/3; // We'll be using 3 by 2 aspect ratio in Portrait mode
 // "Aspect" is width divided by height
 
 var cameraVerticalFOV = camera.fieldOfView; // The camera FOV is the verticall one
 var cameraHorizontalFOV = camera.aspect * cameraVerticalFOV;
 
 var horizontalDesignFOV = designAspect * cameraVerticalFOV;
 // The verticall FOV is the same because the camera fixed height

 var multiplier =  (horizontalDesignFOV / cameraHorizontalFOV);
 // Multiplies the the vertical camera FOV into the vertical design FOV
 
 camera.fieldOfView *= multiplier; 
 // Apply the multiplier

By Zooming the camera by moving it foward or backwards

Here's an example problem to give you a Concept

The Camera doesn't see the full Object like you designed before because of the Camera Dynamic Width alt text You need to calculate The Distance to Move the Camera so it can see the full object

Let's call the line with dashes on it the Meetup Line

P is the position of the T on the Z-Axis

T is half of the MeetUp Line

and D is the distance between a Camera and the MeetUp Line

alt text

You just need to find the Difference of D in Camera A and Design Camera

Here's how you can find it :

First, calculate the T

using the D in the Designed Camera, let's say it's "10 Meters"

 var designHorizontalFOV = designAspect * camera.fieldOfView;
 
 var designTheta = designHorizontalFOV / 2;
 
 var T = Mathf.Tan(Mathf.Deg2Rad * (designTheta)) * 10;
 // "Deg2Rad" convert Degrees to Radians

Then,

You'll need to calculate D for Camera A

 var horizontalCameraAFOV = camera.aspect * camera.fieldOfView;

 var horizontalCameraATheta = horizontalCameraAFOV / 2;

 var cameraA_D = T / Mathf.Tan(Mathf.Deg2Rad * horizontalCameraATheta); // Get the "D"

After That,

you just need to Subtract and Move the Camera

 var camera_A_Displacement = cameraA_D - designCamera_D;
 // The difference betwen Two "D"
 
 camera_A.transform.localPosition = new Vector3(0,0,-camera_A_Displacement);
 // Minus sign to Move the Camera Backwards

If you prefer this method you can also Give a parent to the Camera, so you can move the camera or rotate it without Affecting its zoom by moving or rotating the Parent instead of the Camera directly

I Personally prefer the Second One because i don't need to Mess up with the FOV

The screen orientation on this Answer is Portrait

If there's a mistake that i made or any explanation that you don't understand, Please tell me.


bitmap-01.png (184.9 kB)
bitmap-2.png (101.4 kB)
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 DominoOne · Apr 06, 2016 at 02:57 PM

I know it's an old thread, but people might still end up looking for an answer.

For orthographic cameras it's much easier and has been answered already. For perspective ones, however, you need some trigonometry. All the necessary info about that is here: https://en.wikipedia.org/wiki/Angle_of_view

If you want an easy way out, though, here's a solution that works with all camera types: https://assetstore.unity.com/packages/tools/camera/camera-fit-screen-handler-57439?aid=1101l3r5C

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

15 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

Related Questions

How to stretch the camera view and display to the screen 0 Answers

Camera view different screen sizes? 0 Answers

Camera.ScreenToWorldPoint Not Giving Right Point 3 Answers

Distance gameobject to camera edge 1 Answer

Clicking Trigger 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