- Home /
Negative results when converting HFOV to VFOV?
So I'm trying to match the FOV between blender's horizontal FOV and Unity's vertical.
I found a lot of similar equations in the web on how to do it: 1, 2, 3
My script:
public class H2V_FOV : MonoBehaviour
{
public float vFov, hFov = 60.0f;
void Update()
{
var c = Camera.main;
vFov = 2 * Mathf.Atan(c.aspect * Mathf.Tan(hFov / 2));
c.fieldOfView = vFov;
}
}
However; the result is always negative for some reason :(
The last link (3) gives me the right VFOV if I plug the values in the equation, for ex: width = 491, height = 368, hfov = 60 => vfov = 47 which seems to look correct when testing it.
What am I doing wrong? I tried (float)Screen.width / Screen.height
but same result.
Thanks for any help.
Answer by HarshadK · Jan 13, 2015 at 12:54 PM
The formula in link #3 is actually correct but it is for getting the value of HFOV based on VFOV. What you need is the value of VFOV and not HFOV so I solved that formula to get the value of VFOV from HFOV and that formula becomes:
vFov = Mathf.Atan(Mathf.Tan(hFov/2)/c.aspect)*2;
Plus the angles used in the formula are required in radian as per the same article from link #3. So using some conversions between degrees and radians the code becomes:
public class H2V_FOV : MonoBehaviour
{
public float vFov, hFov = 60.0f;
void Update()
{
var c = Camera.main;
vFov = Mathf.Atan(Mathf.Tan(Mathf.Deg2Rad * hFov/2)/c.aspect)*2;
c.fieldOfView = vFov * Mathf.Rad2Deg;
}
}
I verified the values using the same calculator from that same link and output is actually correct.
Thanks. That's pretty clever. I got confused, cause the other links seemed to have the same equation I had, so I came to the conclusion that the third link had a typo, since it's calculating the vfov, but in the equation they're calculating hfov, why would they do that? psh