Camera only fits part when viewport is scaled on X axis

My camera fitter script only fits when the viewport scales on the x axis but not the y axis. Why does it do this?

Here’s my script:

local Camera = workspace.CurrentCamera

function Fit(Model, CameraPart) -- camera fits model, camerapart is the object thats cframe is being changed
	
	Camera.FieldOfViewMode = Enum.FieldOfViewMode.Diagonal
	
	local ViewportSize = Camera.ViewportSize
	local RadFov = math.rad(Camera.FieldOfView)
	local ModelExtents = Model:GetExtentsSize()

	local BoundingY = ModelExtents.Y
	local BoundingX = ModelExtents.X
	local BoundingZ = ModelExtents.Z

	local Biggest = math.max(BoundingY,BoundingX, BoundingZ)
	local fovx = 2 * math.atan( math.tan(RadFov * 0.5) * (ViewportSize.X / ViewportSize.Y))
	local XOffset = (Biggest * 0.5 )*(1/math.tan(fovx * 0.5))
	
	local FinalCFrame = Model.BL.CFrame:Lerp(Model.TR.CFrame,0.5) - Vector3.new(XOffset + Model.TR.Size.X/2, 0, 0)
	CameraPart.CFrame = FinalCFrame -- reposition the camera part (camera cframe is set to camera parts cframe)

end

Here’s what it does:

1 Like