Npc doenst fit in viewport frame

im made an module that dynamicaly load npcs but now i came across iseus where it cant fit in viewports like it puchec=s the npc to the corner and small instead of corectly fitting it

function NPCFramework:CreateViewportThumb(viewportFrame)
	if not viewportFrame then return end
	
	-- clear curent world models
	for _, child in pairs(viewportFrame:GetChildren()) do
		if child:IsA("WorldModel") then
			child:Destroy()
		end
	end

	-- create a new world model
	local world = Instance.new("WorldModel")
	world.Parent = viewportFrame

	-- create a new npc and apply hum description to it
	local npc = self:GetNewNPC(Enum.HumanoidDisplayDistanceType.None, Enum.HumanoidHealthDisplayType.AlwaysOff, Enum.NameOcclusion.NoOcclusion)
	npc.Parent = world
	self:ApplyHumDescription(npc)

	-- make sure it have an primary part if somehow failed to get an primary part
	if not npc.PrimaryPart then
		npc.PrimaryPart = npc:FindFirstChild("HumanoidRootPart") or npc:FindFirstChildWhichIsA("BasePart")
	end
	if not npc.PrimaryPart then return end

	local cf, size = npc:GetBoundingBox()
	npc:PivotTo(CFrame.new() * CFrame.new(-cf.Position))

	-- add an new camera
	local cam = Instance.new("Camera")
	cam.Parent = world
	viewportFrame.CurrentCamera = cam

	local maxDim = math.max(size.X, size.Y, size.Z)
	local fov = cam.FieldOfView
	local distance = (maxDim / 2) / math.tan(math.rad(fov / 2)) * 1.2 

	local target = Vector3.new(0, size.Y/2, 0)

	local camPos = target + Vector3.new(0, 0, distance)
	cam.CFrame = CFrame.new(camPos, target)
end

i tried to search everywhere but it seems viewports are just trash i tried to fit it manualy but that also doesnt work shouldnt it just fit if its set at 0 position no then it just vanish

any help is apriciated ty

npc gui viewportframe help oop scripting model pivot-point

1 Like

I’m not sure what math you’re trying to do in the camera calculation but try this:

function NPCFramework:CreateViewportThumb(viewportFrame)
	if not viewportFrame then return end

	-- clear curent world models
	for _, child in pairs(viewportFrame:GetChildren()) do
		if child:IsA("WorldModel") then
			child:Destroy()
		end
	end

	-- create a new world model
	local world = Instance.new("WorldModel")
	world.Parent = viewportFrame

	-- create a new npc and apply hum description to it
	local npc = self:GetNewNPC(Enum.HumanoidDisplayDistanceType.None, Enum.HumanoidHealthDisplayType.AlwaysOff, Enum.NameOcclusion.NoOcclusion)
	npc.Parent = world
	
	self:ApplyHumDescription(npc)

	-- make sure it have an primary part if somehow failed to get an primary part
	if not npc.PrimaryPart then
		npc.PrimaryPart = npc:FindFirstChild("HumanoidRootPart") or npc:FindFirstChildWhichIsA("BasePart")
	end
	if not npc.PrimaryPart then return end

	local cf, size = npc:GetBoundingBox()
	npc:PivotTo(CFrame.new(-cf.Position))

	-- add an new camera
	local cam = Instance.new("Camera")
	cam.Parent = world
	
	viewportFrame.CurrentCamera = cam

	local camCFrame = npc:GetPivot() * CFrame.new(0, 0, -3)
	cam.CFrame = CFrame.lookAt(camCFrame.Position, npc:GetPivot().Position)
end

add an offset to the already calculated camPos