Search Unity

Camera FOV bug or intended behaviour?

Discussion in 'Editor & General Support' started by WereVarg, Sep 29, 2016.

  1. WereVarg

    WereVarg

    Joined:
    Apr 9, 2012
    Posts:
    20
    I want my object to cover all the Viewport. There is a simple formula using arctan to calculate the box scale depending of the camera FOV and the distance. The more FOV is the more space i get between box and the view margins. Why? Is it Mathf.Atan fault or the Unity camera behaviour?
    Code (csharp):
    1.  
    2. public class TestFOV : MonoBehaviour
    3. {
    4.  
    5.     private Camera cam;
    6.     public Transform cube;
    7.  
    8.     private float h;
    9.  
    10.     void Start()
    11.     {
    12.         cam = GetComponent<Camera>();
    13.     }
    14.     void Update()
    15.     {
    16.         h = Mathf.Abs(cam.transform.position.z - cube.position.z);
    17.  
    18.         float hFOVinRad = cam.fieldOfView/2f*Mathf.Deg2Rad;
    19.  
    20.         float width = h*Mathf.Atan(hFOVinRad)*2f;
    21.  
    22.         cube.transform.localScale = new Vector3(1,width,.1f);
    23.     }
    24. }
    25.  
    upd. float width = h*Mathf.Atan(hFOVinRad)*2f; should be Tan sorry for bothering
     
    Last edited: Sep 30, 2016
  2. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967