Math for ViewportFrame

Hello Devforum members!
I’m developing a game, and I need help with ViewportFrame. How ViewportFrame, Camera, etc. works, I know, but I’m faced with the problem that I’m constantly changing models in my game, so updating each new model for ViewportFrame is unnecessary work for me.
Therefore, I have been trying for a long time to make an ideal formula that will help me move the object away as much as I need it.

Suppose I have two models, they both have different sizes, but both models are on the same coordinates(Vector3.new(0,0,0)).
The size of the model can be found as follows: local _, size = model:GetBoundingBox

image
Size: Vector3.new(6,9,2).
The model itself is located at the coordinates(Vector3.new(0,0,12.5)), the model is located there because it is the ideal position for this model in the ViewportFrame.
How can I get 12.5 knowing these numbers(Size)?

image
Now this model.
Size: Vector3.new(16,15,2).
Located: Vector.new(0,0,20.83333…)
How can I get 20.8333?

If anyone has any ideas on how to implement this, please write. I apologize for my English, because it is not my native language. Thank you all in advance for your help.

basically, the bigger models gets, the far it goes from the camera.
you can simply get the highest axis of size (X, Y, Z) and move it from camera at certain offset, for example for the first case it would be 3.5 (12.5 - 9 = 3.5)
for second example, the ideal position would be 16 + 3.5 = 19.5.
just play with it

use model:GetExtentsSize()
it retuns the “size” of a model

1 Like

image

The distance from the camera to the model would be determined by using the FoV and the size of the model.

You can also refer to this : Roblox: Fit model perfectly into ViewportFrame - YouTube

3 Likes

Oh, yes, I didn’t know exactly how FieldOfView was supposed to be depicted, but you enlightened me. Thank you and the others who helped as much.