[How-To] Horizontal Field Of View for Unity3d camera

By | November 13, 2014

Advertisement from Google

In Unity3d you can change only vertical field of view for a camera by default. This means whenever you change height unity will automatically scale environment. But if you change width unity will just add some space (or decrease) to left and right side of the screen (no content resizing).

If you want to have opposite behavior, so when you increase width all content will  be scaled and if you increase height only space added to the view, you need to modify camera fieldOfView field.

Here is a simple component that do this:


Advertisement from Google

public class HorizontalFOV : MonoBehaviour {
public float fieldOfView = 60;
 
void Update () {
    camera.fieldOfView = fieldOfView / ((float)camera.pixelWidth / camera.pixelHeight);
    }
}

You can also modify it a bit to have the same view for default aspect ration:

public class HorizontalFOV : MonoBehaviour {
public float fieldOfView = 60;
 
void Update () {
    camera.fieldOfView = fieldOfView * (16f/9f) / ((float)camera.pixelWidth / camera.pixelHeight);
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *