Calculate the distance the camera has to be but also adapting to the viewport size [SOLVED]

This sounds simple but isn’t,
what I want to do is to have a max X and max Y plane so in this case 78.25 Y studs and 139.5 X studs and make the camera see the whole part ALWAYS!

So in other words this plane represents the perfect viewport frame size:

And this represents what I want to happen when the Viewport size X is not perfect and the camera goes further away to adapt to it:

I’ve been able to calculate the Y by: (78.25 / math.tan( math.rad(1)))
But the X seems impossible…

Please help me on this!

Camera Properties:
FieldOfView: 1

I did it!
For anybody trying to do the same here’s all you need to do : )

local DistanceX = 139.5 / math.tan(math.rad(1))/(game.Workspace.Camera.ViewportSize.X / game.Workspace.Camera.ViewportSize.Y) -- Cauculating the X distance of X
local DistanceY = (78.25 / math.tan(math.rad(1))) -- Cauculating the X distance of Y

local ChosenDistance = math.max(DistanceX, DistanceY) -- Chooses the biggest value of X and Y
game.Workspace.Camera.CFrame = CameraPart.CFrame * CFrame.new(0,0, ChosenDistance)

If you continuously update these values the camera will always fit the part inside the viewport size!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.