Viewport Frame Help

I am trying to place a model into a viewport frame however it doesnt work how I want it to…
I want it to be a good picture of a computer that I put into it.
I have tried commenting out

part.Position = Vector3.new(0, 0, 0)

but it looks like this when I do
image

and when I dont comment it out it looks like this
image
but the model that should be showing is this and I have no idea why it shows that-
image

The script:

function viewPortCamera(frame,model)
	local viewportFrame = Instance.new("ViewportFrame")
	viewportFrame.Size = UDim2.new(0, 208,0, 141)
	viewportFrame.Position = UDim2.new(0,0,0,0)
	viewportFrame.BackgroundTransparency = 1
	viewportFrame.Parent = frame

	local part = model.ViewportPart
	part.Position = Vector3.new(0, 0, 0)
	model:Clone().Parent = viewportFrame
	
	local viewportCamera = Instance.new("Camera")
	viewportFrame.CurrentCamera = viewportCamera
	viewportCamera.Parent = viewportFrame

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

for _,v in pairs(game.ServerStorage.Setups:GetChildren()) do
	if v.ClassName == "Model" then
		if script.Parent:FindFirstChild("Template") then
			script.Parent["Template"].Buy.Text = v.Cost.Value.." R$"
			script.Parent["Template"].LayoutOrder = v.Cost.Value
			viewPortCamera(script.Parent["Template"],v)
			script.Parent["Template"].Name = v.Name
		else
			for _,f in pairs(script.Parent:GetChildren()) do
				if f:FindFirstChild("Template") then
					local clone = f:Clone()
					clone.Name = v.Name
					clone.Buy.Text = v.Cost.Value.." R$"
					clone["Template"]:Destroy()
					clone.LayoutOrder = v.Cost.Value
					clone.Parent = script.Parent
					viewPortCamera(clone,v)
				end
			end
		end
	else
		print(v.Name.." is not a model.")
	end
end

Plz help me

2 Likes

Shouldn’t you be positioning the model instead of 1 part?

1 Like

I’ve not worked a lot with viewports, but you set the parts position to 0,0,0 which is the wrong way of doing it.

What you have to do, is to get the CFrame of the ViewportCamera, and then offset the Part with * studs in the lookVector.

So something like this I think

Part.CFrame = ViewportCamera.CFrame + ViewportCamera.CFrame.LookVector*10 -- 10 being the offset

That will place the part 10 studs in front of the viewport frames camera.

And for models, the same deal, just alittle different.

Model:PivotTo(ViewportCamera.CFrame + ViewportCamera.CFrame.LookVector*10) -- 10 being the offset
1 Like

No. The part I am moving is to assist the viewportCamera is being placed.

1 Like

I understand the code, I however do not understand what you want to to do with

Model:PivotTo(ViewportCamera.CFrame + ViewportCamera.CFrame.LookVector*10)
1 Like

Well, This is moving your model 10 studs in front of the camera(viewport camera) as mentioned, if this wasn’t what you were looking for, then please re-explain it for me. :slight_smile:

1 Like

No its fine but it this supposed to happen?
image
and the other ones dont even show…

1 Like

Try changing the number to something lower like 3.

In theory you gotta offset it to something like this:

local ModelSize = Model:GetExtentsSize()
ViewPortCFrame + ViewPortCFrame.LookVector*(Offset+(ModelSize.Z/2))

But then you gotta take in account the orientation of the model that you’ve pasted in, and make sure it’s also facing the camera.

Facing the camera will not be a problem as I plan to make it spin.

And where would this go? I don’t think I have a variable named ViewPortCFrame, but if I did, just using “+” would not add them so that also confuses me. :sob:

Hi!

Okay let me explain what’s going on!

local ModelSize = Model:GetExtentsSize() -- Get the size of our model in all axis. (Like saying Part.Size, just on a model)
local ViewportCFrame = ViewportCamera.CFrame -- ViewportCFrame is actually just our viewport cameras CFrame.
Model.CFrame = ViewportCFrame + ViewPortCFrame.LookVector*(Offset+(ModelSize.Z/2)) -- Let's take this one in steps:
-- Step 1: "ViewportCFrame" Do so our Model's CFrame is the same as our ViewportCFrame. 
-- Step 2: "ViewPortCFrame.LookVector" We prepare to move the Model in the direction of where the Camera is looking. Since LookVector is the forward direction of a object.
-- Step 3: "Offset" is how far away from the Camera we want our Model to be placed.
-- Step 4: "ModelSize.Z/2" Here we're getting the Model's Size in the Z axis (before rotating it and such), and then taking half that size, and adding it to our offset. That does so that no matter of small or large our object is, it will keep the offset at the offset amount we've chosen.


On the image here, you can especially see it in action. :slight_smile:

I am supposed to replace “Offset” with an offset?

Also there is an error with using look vector.

Hi!

It’s because you try to get the LookVector of “Camera” and not “Camera.CFrame” try again and use Camera.CFrame.LookVector! :smiley:

Yeps! the Offset you choose, could be any number.

Ok it makes me feel REALLY guilty to say this and also REALLLLY bad but I decided to close the support thread because I found a viewport generator plugin (already scanned for viruses) and I just decided to use that. I will give you the solution tho for all your help. Ty.

That you found a solution yourself is even better! Feel free to return with any further quesitions. :slight_smile: