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!

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.

Try this…

Camera.ViewportToWorldPoint

You can use
lowerBound = Camera.main.ViewportToWorldPoint(new Vector3(0,0,0)); upperBound = Camera.main.ViewportToWorldPoint(new Vector3(1,1,0));

Hope this helps.

There’s this post in Unity’s manual that tells you how to get the frustum size at a certain distance of the camera: Unity - Manual: The Size of the Frustum at a Given Distance from the Camera

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.