- Home /
Is there a way to set the horizontal field of view of a camera?
Thanks in advance.
Answer by hargisss · Mar 04, 2015 at 04:56 PM
I just had the same problem. Drussilla's code is a good approximation, but is not exact. This Javascript code should be precise:
var hFOV : float = 30;
function FixedUpdate() {
var hFOVrad : float = hFOV*Mathf.Deg2Rad;
var camH : float = Mathf.Tan(hFOVrad*.5)/camera.aspect;
var vFOVrad : float = Mathf.Atan(camH)*2;
camera.fieldOfView = vFOVrad * Mathf.Rad2Deg;
}
Simply copy the code to a new javascript and attach it to the camera.
@hargisss Is there anyway to increase the horizontal field of view without distorting the image?
Hi, zerophase. I'm a little unclear as to what you mean. What are you trying to do specifically?
@hargisss Thanks for the quick response. I'm making a racing game, and the default viewing angles of the camera limits the peripheral distance seen when in first person. I'm trying to make the camera show cars passing on the left and right side a bit sooner.
This post goes into greater detail on my issue; http://answers.unity3d.com/questions/1144842/adjusting-viewing-angle-without-distortion.html
Answer by Statement · Dec 27, 2013 at 08:20 PM
You can set the vertical field of view easily (though you asked for the horizontal and I know of no simple setter for that).
I think you can set the vertical field of view to derive the horizontal field of view, or you could possibly look into messing with the cameras projection matrix to customize for your needs.
Answer by drussilla · Nov 12, 2014 at 11:14 PM
To se horizontal field of view for a camera you should create component with following logic:
camera.fieldOfView = 60 * (16f/9f) / ((float)camera.pixelWidth / camera.pixelHeight);
where 60 is your target field of view value. and 16f/9f - default aspect ration (so the same value for horizontal and vertical field of view give the same image) Here you can find more information.
Your answer
Follow this Question
Related Questions
Camera movement in a 2D? (like angry birds) 1 Answer
Find out if GameObject is in Field of View 2 Answers
Negative results when converting HFOV to VFOV? 1 Answer
Automatically adjust cameras FOV to maintain a perfect 360 coverage with multiple cameras 0 Answers
How to animate zooming in of the camera? 4 Answers