[SOLVED] Making A Model Fit The Whole Viewport Frame

Hello,
I am working on a system for my game, and it requires a model in a viewport. However, I got the viewport itself to work, but I cannot get it to face/fit the whole frame. As you can see in the game picture below, it’s very zoomed out. The other picture of the pipboy 2000 is how I want the model to fit the viewport. Here is my code:

local viewportFrame = Instance.new("ViewportFrame")
viewportFrame.Size = UDim2.new(0.3, 0, 0.4, 0)
viewportFrame.Position = UDim2.new(0, 15, 0, 15)
viewportFrame.BackgroundColor3 = Color3.new(0, 0, 0)
viewportFrame.BorderColor3 = Color3.new(0.6, 0.5, 0.4)
viewportFrame.BorderSizePixel = 2
viewportFrame.BackgroundTransparency = 0.25
viewportFrame.Parent = script.Parent

local part = script.Parent.Model.CamPart
script.Parent.Model.Parent = viewportFrame

local viewportCamera = Instance.new("Camera")
viewportFrame.CurrentCamera = viewportCamera
viewportCamera.Parent = viewportFrame

viewportCamera.CFrame = CFrame.new(Vector3.new(0, 2, 12), part.Position)

Any help would be appreciated!
PipBoy2000

2 Likes

I think this is what you are looking for:

1 Like

That didn’t work, I tried it before but it doesn’t work how I want it to.

It really should because it will fit the model regardless of the orientation.(if the pivot point is correct)
Or if you want a simpler method then just use simple trigonometry.

I understand that, but its really complicated and it is constantly turning and I am not well acquainted with module scripts and it’s not what I am looking for. I do not know what trigonometry is.

Basically this is what you want to solve.


Where L is the longest length. O is opposite and A is adjacent. The FOV is halved to make a right angled triangle. Find out A to get the required distance for the whole model to fit.
image
Rearrange to get a.

local FOV =
local L = 
local D = (0.5*L)/(math.tan(math.rad(FOV/2))

-- and if you want to change the FOV instead then
FOV = math.deg(math.atan((L*0.5)/D)))
--but L and D are required.
1 Like

This video by Sleitnick should solve your problem Roblox: Fit model perfectly into ViewportFrame - YouTube

This may help →

image

You could create a viewport, then have a worldModel inside of it with a Camera.

Then simply →

ContainerClone:WaitForChild("Viewport").WorldModel.PrimaryPart = ToolPreview.Handle
ToolPreview.Parent = ContainerClone:WaitForChild("Viewport").WorldModel
if not ContainerClone.Viewport:FindFirstChildOfClass("Camera") then
	local Camera = Instance.new("Camera")
	Camera.Parent = ContainerClone.Viewport.WorldModel
	local CameraPosiiton = (CFrame.new(ToolPreview.Handle.Position)).Position
	Camera.CFrame = CFrame.new(ToolPreview.Handle.Position + ToolPreview.Handle.CFrame.LookVector * 5, ToolPreview.Handle.Position)
	ContainerClone.Viewport.CurrentCamera = Camera
	ContainerClone.Parent = Container
	if not ToolPreview:FindFirstChild("PartRotate") then
		local ScriptClone = script:WaitForChild('PartRotate'):Clone()
		ScriptClone.Parent = ToolPreview
	end
end

A friend helped me with this, and it may be resourceful to you.

Have you tried installing the place and check out how Ego did it? Because it apparently works if it works for me and many others lol.

Yes, but it didn’t work how I was hoping it did.

I tried this script and messed around with it and didn’t work. I tried making the code work but theres no errors but it just doesn’t appear in the viewmodel.

Wait so the map in the top left is the viewport frame?

Could you try selecting it, and showing us positions, and any properties we can look at.

Im wondering why, it might be the size or it might just be the camera.

Most likely →

viewportCamera.CFrame = CFrame.new(Vector3.new(0, 2, 12), part.Position)
try and mess with the values.

Would you mind providing a model file?

Of the viewport frame and the model?

Yes, of both if possible, thanks.

Alright here you go
pipboy.rbxm (122.4 KB)

pipboy.rbxm (122.9 KB)

image

Thanks a lot man! It works great.