- Home /
Camera FOV and Distance based on mobile screen size
I want to change the height and the FOV of the camera based on the mobile resolution.
The issue is that 1- I am not excellent in math and 2- Some information is missing for me to find the missing pieces. I know one angle and one side, but I need more than that to know how to set the angle FOV and the correct height.
From the picture attached, I know the camera is perpendicular to the screen (so a 90 degree angle) and I can find one side of the triangle by getting the Screen.width/2.
Any idea how I can solve the problem?
Answer by Namey5 · Aug 18, 2019 at 09:00 AM
What exactly is the relationship between the camera's height and field of view? We need one to calculate the other, so unless there is a specific relationship it would be easier to define one and find the other relative to that, i.e.
//Let's say the height is at a constant 10
float height = 10f;
//Now that we know the side lengths of the right triangle, we can find the angles using trigonometry
float width = Screen.width * 0.5f;
float fov = Mathf.Atan (width / height) * Mathf.RadToDeg * 2f;
camera.fieldOfView = fov;
The basic idea is that;
//Where a is the angle we are trying to find
tan(a) = oppositeSide / adjacentSide
//In our case, the opposite side is 'width' and the adjacent side is 'height'
//If we want to find the angle 'a', we can use the inverse of the tan function - arctan (atan)
atan(tan(a)) = a
//But tan(a) = opp / adj as we saw earlier, so we can find the angle that way
atan (width / height)
Then to find the field of view, we convert from radians to degrees and double the angle. Now that we know the field of view, you can adjust the height with whatever ratio you want relative to the fov.
Hello @Namey5 and @mChapuis I am working on what appears to be a similar issue. I've used the math you laid out in this thread but am wondering what I'm missing.
I first specify the "height" which, if I understand correctly, is the distance between the camera and the player/map (not the height of the screen in pixels). This distance uses world units (aka uses values from translate.position) so perhaps this needs to be converted?
I attempted to use the formula you showed above but I'm getting FOVs which are either way too far zoomed out or way too far zoomed in.
To make sure I'm clear in my goal: No matter the device resolution I'd like to show the same amount of world map but zoom in or out depending on the device resolution.
Any pointers would be greatly appreciated!!
Height is just a scaling factor to deter$$anonymous$$e the bounds of the screen. If you want to use a world-space height, then you will need to scale the resolution appropriately;
//Assu$$anonymous$$g default width of 1080 pixels, where 'contentWidth' is the world-space size of the content you want to show
float width = (Screen.width * 0.5f / 1080f) * contentWidth;
However, that isn't really what the above is for to begin with. The field of view is by default dependant on aspect ratio, not resolution, therefore the content on screen should stay the same regardless of resolution so long as your aspect ratio is the same.
Thank you so much for the quick reply! Have it working now :) VERY much appreciate it!
Answer by UnityGuy1717 · May 20, 2020 at 12:15 AM
@Namey5 Upon further investigation I realized that my implementation fixed the horizontal size so it's the same across screen resolutions but the vertical is not remaining constant. I found this article which led me to current approach, but I'm still having trouble getting the camera content to remain consistent across resolutions. (https://answers.unity.com/questions/778670/fixed-width-relative-height-on-different-aspect-ra.html)
Is it possible to fix the width and height of camera view so that the experience is the same across devices with different aspect ratios? Is a border/pillbox approach needed? Again, thank you very much for your time!!
Your answer
Follow this Question
Related Questions
Different camera views in different devices 4 Answers
Camera "Don't Clear" flag doesn't work on Android ? 1 Answer
Adjust the exposure of phone camera manually 1 Answer
Camera viewport should be square and pixel-perfect, regardless of player aspect ratio. 0 Answers
Adjusting Camera Z Position by Resolution (Canvas Size) 0 Answers