- Home /
How to get Camera's field of view boundaries coordinates?
Hello, guys! Have pretty simple question. You know that camera's field of view can be approached as rectangle. Please give me ANY idea how it is possible to get absolute coordinates of its "vertices"?
Maybe some info about my game goal would be useful for the answer. I need to restrict the area in which I will randomly spawn objects to the field of view of camera, that is, that no objects are spawned beyond camera.
Thanks in advance!
Answer by smoggach · Dec 11, 2014 at 05:35 PM
That's pretty tricky. My approach would be to start with the rectangle instead of trying to find it.
float x = Vector3.Distance(Vector3.zero, Camera.main.transform.position);
float y = height / 2f;
float requiredFOV = Mathf.Atan(y / x) * Mathf.Rad2Deg;
Camera.main.fieldOfView = requiredFOV * 2f;
this code looks at the height of your given rectangle and sets the camera's FOV to show only that rectangle. You could modify this formula to find the rectangle at a given distance but imho it's better adapt your camera to the rect than the rect to the camera.
Answer by Kaushik Banerjee · Sep 04, 2015 at 01:11 PM
Try this..
You can use lowerBound = Camera.main.ViewportToWorldPoint(new Vector3(0,0,0)); upperBound = Camera.main.ViewportToWorldPoint(new Vector3(1,1,0));
Hope this helps.
Answer by DiegoSLTS · Sep 04, 2015 at 02:48 PM
There's this post in Unity's manual that tells you how to get the frustum size at a certain distance of the camera: http://docs.unity3d.com/Manual/FrustumSizeAtDistance.html
You can take a random value betwen near and far planes, get the size of the frustum for that distance and then a random value for that width and height.
With those random values you should always be inside the frustum.
Your answer
