I have a square quad representing a board in my game. I have a perspective camera and want it to fully focus.zoom in on the board. Android devices have various resolution which is making it very hard. The game should be run on portrait orientation so there should be some padding on the up and bottom of the board since the board is a square. But the two sides of the square should match the edge of the screen.
Can anyone suggest me an equation to find the camera’s field of view?
Thanks in advance
It’s some basic trigonometry. The field of view in Unity cameras is the angle between the camera’s forward axis and the top or bottom side of the screen (i.e. vertical field of view). To calculate this angle, you simply go
Vector3.Angle(transform.forward, GetComponent<Camera>().ViewPortToWorldPoint(new Vector2(0.5f,1)) - transform.position);
Because you want the edges of the screen to match the sides of the square instead of the top and bottom, you’ll have to do an extra step. Instead of calculating the angle between the top of the screen and the center, you have to calculate it between the side of the square and the center, and divide it by the aspect of the camera:
Vector.Angle(transform.forward, squareSideCoords - transform.position) / GetComponent<Camera>().aspect;
squareSideCoords need to on the side of the square, obviously, but also in the vertical centre of the camera, or else the angle won’t be correct. Since your camera will doubtlessly be centred above the square this shouldn’t be hard to do in your case though.