Hello developers. I made this post because my viewport frame isn’t showing the models I want it to. From a script, I am cloning all of the models in a certain folder, and then showing them in a viewport frame. However, it is not working. (ViewItem is a model under the viewport frame.)
for i, v in pairs(builds:GetChildren()) do
if v:IsA("Model") then
local newViewPort = viewPortFrame:Clone()
newViewPort.Visible = true
newViewPort.Parent = buildsFrame
local viewPortBuild = v:Clone()
viewPortBuild.Parent = newViewPort.ViewItem
local newCam = game.Workspace.CurrentCamera:Clone()
newCam.Parent = newViewPort
newViewPort.CurrentCamera = newCam
newViewPort.CurrentCamera.CameraType = Enum.CameraType.Scriptable
newCam.CameraSubject = viewPortBuild
end
end
Hey, here’s a basic example of how you can show a model in a ViewPort frame:
-- First setup a camera in your ViewportFrame
local viewport = viewPortFrame
local ViewportCamera = Instance.new('Camera', viewport) -- let's make a new camera under the viewportframe
ViewportCamera.CameraType = Enum.CameraType.Scriptable
ViewportCamera.FieldOfView = 10 -- change accordingly
viewport.CurrentCamera = ViewportCamera
-- Then put your Model "ViewItem" in the ViewPort and adjust your camera
local model = ViewItem
model:SetPrimaryPartCFrame(CFrame.new(0, 0, 0))
model.Parent = viewport```
Make sure the model is in the correct position. The models could have different positions and therefore are not showing up.
Use model:PivotTo(CFrame.new(CFRAME POS HERE)) to achieve this.
Hello, right now the objects look like this, and also, every model is a different size, so is there a way I can make this look better (because it doesn’t look very good right now)
local modelOrientation = 45
model:SetPivot(CFrame.new(0, 0, 0)*CFrame.Angles(0, math.rad(modelOrientation), 0))
and set the camera CFrame by using
local camDistance = 3
viewportCam.CFrame = CFrame.new(Vector3.new(camDistance, camDistance, camDistance), model:GetPivot().Position)
-- replace viewportCam with the name of the variable storing the viewport camera/viewport.CurrentCamera
This will make it so the model in the viewport frame is farther from the camera and is orientated, and you can customize this using the variables or further modifying it the code.
for i, v in pairs(builds:GetChildren()) do
if v:IsA("Model") then
local newViewPort = viewPortFrame:Clone()
newViewPort.Visible = true
newViewPort.Parent = buildsFrame
local viewPortBuild = v:Clone()
viewPortBuild.Parent = newViewPort
local modelOrientation = 45
viewPortBuild:GetPivot(CFrame.new(0, 0, 0)*CFrame.Angles(0, math.rad(modelOrientation), 0))
local newCam = Instance.new("Camera")
newCam.Parent = newViewPort
newCam.CameraType = Enum.CameraType.Scriptable
newCam.FieldOfView = 10
newViewPort.CurrentCamera = newCam
newCam.CameraSubject = viewPortBuild
local camDistance = 3
newCam.CFrame = CFrame.new(Vector3.new(camDistance, camDistance, camDistance), viewPortBuild:GetPivot().Position)
viewPortBuild:PivotTo(CFrame.new(0, 0, 0))
end
end
Also, let me know if you need more code than that, though I doubt you will. (There is just the function and a few more lines that delete the viewports previously in the frame first)