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 = Instance.new("ScreenGui")
ui.Parent = game.Players.LocalPlayer.PlayerGui

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

local viewportCamera = Instance.new("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
end


viewportCamera.CameraType = Enum.CameraType.Scriptable

while true do
	wait(.017)
	local camposition = Vector3.new(game.Workspace.TestCam.CAMERA.Position)
	
	local camrotation = Vector3.new(CFrame.Angles(math.rad(game.Workspace.TestCam.CAMERA.Orientation.X), math.rad(game.Workspace.TestCam.CAMERA.Orientation.Y), math.rad(game.Workspace.TestCam.CAMERA.Orientation.Z)))
	
	viewportCamera.CFrame = CFrame.new(camposition, camrotation)
end

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 CFrame.new 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
    wait(.017)
    local workspaceCameraPart = game.Workspace.TestCam.CAMERA
    viewportCamera.CFrame = workspaceCameraPart.CFrame
end

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")

RunService.RenderStepped:Connect(function()
    local workspaceCameraPart = game.Workspace.TestCam.CAMERA
    viewportCamera.CFrame = workspaceCameraPart.CFrame
end)

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.