Setting viewport's camera to a part

Hello! I’m trying to set my viewport’s camera to a part’s position, however, I’ve encountered an issue. It doesn’t seem to work. I’ve tried a lot of methods, including adding a WorldModel, but nothing seems to help.

local CamerasFolder = script.Parent:WaitForChild("Cams")
local viewportFrame = script.Parent
local worldModel = viewportFrame:FindFirstChild("WorldModel")

task.spawn(function()
	repeat task.wait() until #CamerasFolder:GetChildren() > 0

	for _, camPart in pairs(CamerasFolder:GetChildren()) do
		camPart.Archivable = true
		local clonedPart = camPart:Clone()
		clonedPart.Parent = worldModel

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

		game:GetService("RunService").RenderStepped:Connect(function()
			viewportCamera.CFrame = clonedPart.PrimaryPart.CFrame
		end)

		task.wait(1)
	end
end)

Here’s the structure:

2 Likes

Hi, @Rapture_TY.

I think Camera is inside in part’s location, not far away enough to see it.

Try to put camera’s location to bit far away and right then see results. :slightly_smiling_face:

Does the code manage to create the viewportframes? I’m presuming so, assuming that the structure photo provided is in a playtest. (Asking this just in case the code never actually gets past the repeat until line)

Secondly are you trying to make a CCTV type system? Do the parts representing cameras in the CamerasFolder move or stay static? I also ask this because if the positions of the cameras never actually change, you don’t have to set the viewportCamera’s CFrame every frame, just when you initially create it!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.