How to know if a part is entirely in a camera's field of view?

Context: I have a viewport frame (UIAspectRatioConstraint of 1) with the current camera’s FOV at 1, so that it’s as orthographic as possible.

If the part in the viewport frame is quite big, then I need the camera to be really far. If the part is really small, I need the camera to be quite close so that the part is visible. How do I create a script or formula that moves the camera back so that the part is entirely visible, with some margins? Thanks!

image

image

take a look at this video, uses a method that might be the solution you’re looking for

This unfortunately didn’t work, but creating my own function by graphing the results of incrementing did. Thanks for the help though! :D