I need help with viewport frames camera ,I want to do the same as I do with the normal camera but it doesnt work with viewportframe I have a video of it.It is supposed to show the noob that I put in the viewport frame but the camera is different from the other one despite being a clone as you can see.
1 Like
Make sure that CurrentCamera (a ViewPortFrame property) is set to the camera you want to use.
I have a script that fixes this for you; are you doing it in a localscript?
if that works what I did was cloning the currentcamera and setting the viewport frame camera to this one
yes it’s in a local script ,canI see the script please?
local function createHeadshotFromModel(model, viewportFrame)
-- Clear frame
viewportFrame:ClearAllChildren()
-- Create camera
local viewportCamera = Instance.new("Camera")
viewportCamera.Name = "ViewportCamera"
viewportCamera.CameraType = Enum.CameraType.Scriptable
viewportCamera.FieldOfView = 30 -- Adjusted Camera FoV
viewportCamera.Parent = viewportFrame
viewportFrame.CurrentCamera = viewportCamera
-- Clone model and parent to ViewportFrame
local clone = model:Clone()
clone.Name = "HeadshotClone"
clone.Parent = viewportFrame
-- Define the origin point for the model
local viewPoint = Vector3.new(0, 0, 0)
-- Ensure a PrimaryPart exists, preferably the Head
if not clone.PrimaryPart then
local primary = clone:FindFirstChild("Head") or clone:FindFirstChildWhichIsA("BasePart")
if primary then
clone.PrimaryPart = primary
else
warn("createHeadshotFromModel: Could not find a suitable PrimaryPart (Head or BasePart) for model:", model.Name)
return -- Exit if no primary part can be assigned
end
end
-- Position model at the origin AND make it face forward (along the world +Z axis within the viewport)
local forwardDirection = viewPoint + Vector3.new(0, 0, 1) -- A point slightly in front of the origin
clone:SetPrimaryPartCFrame(CFrame.lookAt(viewPoint, forwardDirection))
-- Variable to hold the RenderStepped connection
local connection = nil
-- Align camera dynamically using RenderStepped
connection = RunService.RenderStepped:Connect(function()
-- Check if the clone and its parent ViewportFrame still exist
if not clone or not clone.Parent or not viewportFrame or not viewportFrame.Parent then
if connection then
connection:Disconnect() -- Stop listening if the clone or frame is gone
connection = nil
end
return
end
-- Find the head part
local head = clone:FindFirstChild("Head")
if not head then
-- Keep trying if the head isn't found yet
return
end
-- **Camera Positioning Logic**
local headCF = head.CFrame
local headPosition = headCF.Position
-- Calculate directions relative to the head's orientation
local forwardVector = headCF.LookVector
local upVector = headCF.UpVector
-- Define camera offset parameters (adjust these for desired framing)
local cameraDistance = 3.5 -- How far in front of the face
local cameraHeightOffset = 0.2 -- How much higher than the head's center
local cameraSideOffset = 0 -- Optional horizontal offset (positive is right)
-- Calculate the desired camera position relative to the head
local desiredCamPos = headPosition + (forwardVector * cameraDistance) + (upVector * cameraHeightOffset) + (headCF.RightVector * cameraSideOffset)
-- Position the camera at the calculated spot and make it look at the head's position
viewportCamera.CFrame = CFrame.lookAt(desiredCamPos, headPosition)
end)
-- Return the connection so it can be manually disconnected later if needed
return connection
end
call this using your NPC & viewport frame (ex):
createHeadshotFromModel(game.Workspace.NPC, script.Parent.viewportFrame)