Help with viewport frames, Cframe & vector3

Hello! For the past hour I have scoured this forum and other sources for help with my specific problem. Basically, I have a camera inside a viewport frame that uses a camera PART in the workspace to position itself as eventually a player will move this camera, in turn changing the perspective of the viewport frame.

Currently, everything works fine except the fact that my viewport camera isn’t setting the position and orientation from the WORKSPACE camera. Below is my localscript which is in starterplayerscripts for now. How the current system works is that it clones everything from a folder in workspace (which contains everything “in-scene”) to inside the viewport frame. I have it set up to show the baseplate and the tree so far, except as described earlier it doesn’t show the tree and seems to have a mind of it’s own and sets to it’s own position and orientation.

Screenshot 2024-03-29 at 21.59.25
The grey floating part is the camera, the red part can be ignored for now. You can also then see what happens when I view the viewport frame:
Screenshot 2024-03-29 at 22.04.44
As you can see, this is very much angled and higher than the actual position of the WORKSPACE camera.

Also, I am well aware of the lag this way of doing it may cause however I would prefer to get it working first.

I hope you all understand and help would be much appreciated.
I’m new to vector3 and cframe stuff so I’m sorry if I’m just being stupid.

LocalScript in StarterPlayerScript:

local ui ="ScreenGui")
ui.Parent = game.Players.LocalPlayer.PlayerGui

local viewportFrame ="ViewportFrame")
viewportFrame.Size =, 480, 0, 270)
viewportFrame.Position =, 0, 0.52, 0)
viewportFrame.Parent = ui

local viewportCamera ="Camera")
viewportFrame.CurrentCamera = viewportCamera
viewportCamera.Parent = viewportFrame

local scene = game.Workspace.Scene:GetChildren()
for i,v in pairs(scene) do
	local clone = v:Clone()
	clone.Parent = viewportFrame

viewportCamera.CameraType = Enum.CameraType.Scriptable

while true do
	local camposition =
	local camrotation =, math.rad(game.Workspace.TestCam.CAMERA.Orientation.Y), math.rad(game.Workspace.TestCam.CAMERA.Orientation.Z)))
	viewportCamera.CFrame =, camrotation)

if i may ask are there any errors? pardon

Nope, none at all. I’ve checked multiple times and tried it too but no errors.

hmm maybe you need to use runservice.renderstepped

wait try printing the viewport camera position see if it matches with the one on the workspace

1 Like

hi, you’re setting the CFrame of the viewportCamera wrongly.

the function, when given a position vector and a target vector, expects the target to be a point in space that the camera looks at, not a rotation vector. This is maybe why the camera isn’t orienting as expected;

to set the viewportCamera’s position and orientation to match the workspace camera part, you should use the CFrame of the workspace camera part. You don’t need to convert the orientation to angles and then back to a CFrame

i corrected your while loop here:

while true do
    local workspaceCameraPart = game.Workspace.TestCam.CAMERA
    viewportCamera.CFrame = workspaceCameraPart.CFrame

it’s good practice to use RunService.RenderStepped for rendering-related updates instead of a while true do loop with a wait function too

local RunService = game:GetService("RunService")

    local workspaceCameraPart = game.Workspace.TestCam.CAMERA
    viewportCamera.CFrame = workspaceCameraPart.CFrame

hope that this fixes the problem :smile:

noice, yeah that should’ve worked.

Thanks so much! I’ve been looking everywhere for a fix and this was what I needed. <3

1 Like

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