# Ajust FOV to fit objects bounds?

Hi!..
I have a camera orbit that zoom I do from FOV and works really good.

I have a button in scene called “back” to back to an anterior state.

Is possible to calculare the FOV to fit the objects bounds in scene?

I already calculate the boundaries to center camera. But I also want to try to fit the zoom.

I saw some examples online where they adjust distance, transform position that isn’t my case.

Thank you!

Probably but I think Cinemachine has something like that built in or at least has the scripting Api for checking target bounds.

1 Like

Well, it depends on how precise you want that. Getting a precise zoom so the object really just touches the borders of the camera can be very tricky, especially when the objects have unusual shapes. For a precise frame you would need to determine the angle from the center to each vertex of the object and take the largest one. This has to be done for both, the horizontal and vertical axis. Then, taking the aspect ratio into account, determine which one is the larger angle and use that as the fov. There are other ways by only considering the AABB (aka Bounds) of the object. However that is generally larger than the actual object, so the object will appear smaller. Depending on the shape and orientation of the object this could make a huge difference.

A middle ground would be to define a “sphere” that should be focused. So you could temporarily add a sphere as a child, position and scale it so it represents that object nicely in most situations and then just remember that sphere center and radius. With those you could calculate the necessary fov to frame the sphere from the current camera position. That would simplify the situation heavily. The math is extremely easy.

3 Likes

Math is easy hum? hahaha

I got the b value but now can you help me to implement all the others functions in a script?

Thanks

That value is all you actually need. I put all irrelevant stuff in that folder that is just there to visualize the fov with the two lines and display the target circle.

``````public float objectRadius = 3f; // set in inspector
public Transform obj; // set in inspector

// [ ... ]
float dist = Vector3.Distance(obj.position, Camera.main.transform.position);
Camera.main.fieldOfView = fov;
``````

I would have used more meaningful variable names in the desmos drawing, but variables can only be single characters, math, you know ^^.

2 Likes

1 Like

Honestly I’ve just seen games set a minimum distance that objects can be orbited and call it a day.

EVE Online does this. There’s very clearly just a defined radius at which is the minimum distance that you can orbit your camera around an object.

Yes, I’ve seen games that try to be smart about the camera zoom and framing (Space engineers) but it horribly fails at unusual builds. Like a very long station (space elevator for example). The game tries to frame the whole thing which is completely useless as I’m not interested in seeing my station from 3km distance ^^. Though the camera has limits so it just sticks at the furtherst possible point and you can not zoom in at all.

2 Likes

Calling yourself out here? Or did someone else work on the camera?

1 Like

No, I was talking about Space Engineers. It’s like minecraft but in space. You can actually build space crafts or stations with a voxel system and it doesn’t really have a limit how large you can build. Just recently I actually drilled into the center of the moon (about 9km deep) and build an actual elevator shaft to the center Currently I’m about half the way out the other side. When you’re docked to a station, the third person orbit camera will include the station you’re docked to.

1 Like

Holly, works perfectly…That is impressive…Very simple…E forgot to implement Mathf.Rad2Deg2f.

Really thank you…

1 Like

Oh, I thought you had mentioned in the past you worked on the game. Or am I remembering wrong there?

1 Like