# How to calculate horizontal field of view?

Discussion in 'Scripting' started by Jessy, Dec 22, 2008.

1. ### Jessy

Joined:
Jun 7, 2007
Posts:
7,327
Rodolfo-Rubens likes this.
2. ### andeeeee

Joined:
Jul 19, 2005
Posts:
8,768
I think you want something like:-
Code (csharp):
4.

jamesp124, guotata, fxlange and 4 others like this.
3. ### Jessy

Joined:
Jun 7, 2007
Posts:
7,327
Thanks!

I figured out something quite similar on my own, but it took me some researching on Wikipedia about trigonometry, because I haven't really use trig for a decade. However, I didn't know about camera.aspect, so thanks for pointing me to that!

Code (csharp):
2. var cameraHeightAt1 : float = Mathf.Tan(vFOVrad *.5);
3. var hFOVrad : float = Mathf.Atan(cameraHeightAt1 * camera.aspect) * 2;
You'd think that this would be included for convenience, in the API, if something as simple as camera.aspect is. How should I suggest this to UT?

Rodolfo-Rubens, ghiboz and Helical like this.
4. ### andeeeee

Joined:
Jul 19, 2005
Posts:
8,768
I think the Report Bug app has an option on the pop-up to send a feature request. UT are more likely to take notice of requests sent like this than Wish List posts, so they say.

Rodolfo-Rubens likes this.
5. ### drussilla

Joined:
Mar 20, 2014
Posts:
9
If you want to have horizontal fiels of view instead of vertical you need this formula:
camera.fieldOfView = horizontalFieldOfView * (16f/9f) / ((float)camera.pixelWidth / camera.pixelHeight);

Last edited by a moderator: Feb 16, 2019
Rodolfo-Rubens likes this.
6. ### Helical

Joined:
Mar 2, 2014
Posts:
50
6 years later. I find this useful. Took me 10 minutes to understand what you did there. nice 1.

Rodolfo-Rubens likes this.
7. ### Taylor-Libonati

Joined:
Jan 20, 2013
Posts:
15
For anyone interested, I wrote a script that you can attach to a gameObject with a camera and it will log the horizontal FOV whenever it is updated. I used Jessy's formula which seems pretty accurate.

Code (CSharp):
1.
2. using UnityEngine;
3. using System.Collections;
4.
5. [RequireComponent( typeof( Camera ) )]
6. public class ShowCameraHorizontalFOV : MonoBehaviour {
7.     private Camera cam;
8.     private float horizontalFOV = 0;
9.     private float loggedFOV = 0;
10. #if UNITY_EDITOR
11.     public void OnDrawGizmos()
12.     {
13.         cam = gameObject.GetComponent<Camera>();
14.         if (cam != null)
15.         {
16.             horizontalFOV = getHorizontalAngle(cam);
17.             if(horizontalFOV != loggedFOV)
18.             {
19.                 loggedFOV = horizontalFOV;
20.                 logFOV(horizontalFOV);
21.             }
22.         }
23.     }
24.    private  float getHorizontalAngle(Camera camera)
25.     {
27.         float cameraHeightAt1 = Mathf.Tan(vFOVrad * .5f);
28.         return Mathf.Atan(cameraHeightAt1 * camera.aspect) * 2f * Mathf.Rad2Deg;
29.     }
30.
31.     void OnEnable()
32.     {
33.         if(loggedFOV != 0)
34.         {
35.             logFOV(loggedFOV);
36.         }
37.     }
38.     private void logFOV(float value)
39.     {
40.         Debug.Log(name + "FOV: " + value);
41.     }
42. #endif
43. }

8. ### Nodrap

Joined:
Nov 4, 2011
Posts:
66
Found this thread really useful when I wanted to find a way to fix the horizontal field of view regardless of the aspect ratio of the camera. Here's the code by way of thanks. Just drop it onto your camera and set your fixed horizontal FOV.

Code (CSharp):
1. using UnityEngine;
2.
3. // Lock the cameras horizontal field of view so it will frame the same view in the horizontal regardless of aspect ratio.
4.
5. [RequireComponent (typeof(Camera))]
6. public class HorizontalFOVLock : MonoBehaviour {
7.
8.     public float fixedHorizontalFOV = 60;
9.
10.     Camera cam;
11.
12.     void Awake()
13.     {
14.         GetComponent<Camera>().fieldOfView = 2 * Mathf.Atan(Mathf.Tan(fixedHorizontalFOV * Mathf.Deg2Rad * 0.5f) / GetComponent<Camera>().aspect) * Mathf.Rad2Deg;
15.     }
16. }

unityunity