bit of a tricky one - any ideas?

Hey, all,

I’m trying to work out the distance of the field of view from my main camera in the game at a set distance.

So, if I’m interested in a distance of, say, 10.0f, how do I work out the distance (in world space) of the size of the screen (0 to 480 in this case)?

Cheers,

SB

Not quite sure what you mean, but since you have the angle of your FOV on the camera, you can project a vector along the viewport edges to any specific distance and then read back those co-ordinates as world-space, screenspace or whatever.

Perhaps you can explain a little more clearly what you are trying to achieve?

You can dot a vector through the camera matrix.

or even easier use ScreenToWorldPoint

or
ViewportToWorldPoint

What distance are you referring to exactly?

I have an idea

Vector3 a = camera.ScreentoWorldPoint(new Vector3(0, Screen.height/2, 10.0f));
Vector3 b = camera.ScreentoWorldPoint(new Vector3(Screen.width, Screen.height/2, 10.0f)
float dist = Vector3.Distance(a, b);

Let me know if it works for you, may need some adjustments. For instance, the 10.0f is in the direction of the pixel position, but if you wanted the distance to be 10.0f from the center of the screen, that’s more math.

You can do this a bit more simply with a bit of trigonometry. If you imagine looking at the view frustum side-on then you can think of it as two right angled triangles reflected around the centreline (ie, the centre of the view). You can then use Mathf.Tan with half of the FOV angle to calculate half of the screen height which can then, of course, be doubled to get the full height:-

function ScreenHeightAtDistance(fov: float, dist: float) {
  return 2.0 * dist * Mathf.Tan(fov * 0.5 * Mathf.Deg2Rad);
}

The FOV is measured in the vertical axis but you can use it to find the screen width by multiplying the height by the camera’s aspect ratio.

Andeeee, would using trig, Tan specifically, be appreciably slower than just using the vector math?