Calculating FOV with lookat

If I have this camera:

transform.LookAt(cube);

How do I calculate GetComponent().fieldOfView = ??
to fit in the whole screen to fit the cube in on the whole screen when it looks at something. Ive got different sizes on cubes and I want to click on a cube and zoom in on them.

Here’s a step by step solution:

  • Calculate/Retrieve cube bounds.
  • Store the bounds center position(boundsCenter) and extents magnitude(radius). This position and magnitude creates a bounding sphere.
  • Calculate the direction from your camera position to the bounds center(cameraToBoundsCenter). The magnitude of this vector is the length of a base in a right triangle(b).
  • The radius we retreived earlier can be used as the height of a right triangle.
  • arc-tanget of radius/b should give you half the angle(radians) you’re looking for.
  • Just multiply by two and convert to degrees.

Im not looking for angle , Im actually looking for a solution when objects grow smaller micro and what todo with the camera and field of view when they really small.

I thought you wanted the fieldOfView value needed to to frame a box you’re looking straight at, which is what I tried to provide.

ok thanks I understand.

I also thought of another way I could do it by moving camera back and forward untill it hits the screen at the top middle.
Not sure how todo that also.
Thought about shooting a ray off the object to the top of the screen and then taking the distance of it. Do you know the code to shoot a ray to the top of the screen? That would be alternative way I think.
If any of you have code for the angle of field of view please share.

if(Physics.Raycast(transform.position, rayDirection, hit, rayRange))

anyone know what hit is on screen top of the camera field of view?