- Home /
Finding the angle at which the edges of a camera shoot out
So, i have to zoom my camera out so the entire map is on the screen, and i can easily do the calculation of how far that has to be, if just i knew the angles in the camera, how do you find these, and is there a smarter way to do this?
In a meeting can't give explicit example, but you could do this easily (if not micro-optimized) by using:
So you could then:
Get the pixel coordinates of your screen corners.
Cast rays from those coordinates.
If you want the angles component-wise, take the vector3 difference from any of those corners with the camera's transform.forward vector.
Answer by Tasarran · Mar 21, 2017 at 02:39 PM
The Angle Of View is a setting on the camera, directly accessible via: Camera.fieldOfView.
You'll still have to do some further calculation based on the height/width ratio, but this will get you started.
Answer by Frozaken · Mar 22, 2017 at 11:18 AM
For future reference, this is how i ended up solving the problem:
float CalculateZ(){
Ray bottomLeft = Camera.main.ScreenPointToRay (Vector2.zero);
Ray topRight = Camera.main.ScreenPointToRay (new Vector2 (Screen.width, Screen.height));
float tx = (LevelManager.self.gridWidth+2) * LevelManager.self.gridSize/(bottomLeft.direction.x - topRight.direction.x);
float ty = (LevelManager.self.gridHeight+2) * LevelManager.self.gridSize/(bottomLeft.direction.y - topRight.direction.y);
return (ty<tx?ty:tx)*bottomLeft.direction.z;
}
the +2 is for the edges