Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why is my custom camera jittery?

Asked by 5 years ago

I've been working on a custom camera script and I tried to implement a poppercam back into the scriptable camera. I've learned that there's a function GetLargestCutoffDistance() that returns a float, which refers to the number of studs the camera must move forward if there is something blocking it. Here's a little snippet of my code:

runService.RenderStepped:Connect(function()
    local Character = localPlayer.Character
    local rootPart = Character:FindFirstChild("HumanoidRootPart")

    if Character and rootPart then
        Camera.Focus = Character.Head.CFrame

        --How many studs to move forward
        local cutoff = Camera:GetLargestCutoffDistance({})

        --Set rotated start cframe inside head
        local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)
            *CFrame.Angles(math.rad(yAngle), 0, 0)

        --Set camera focus and cframe
        local cameraCFrame = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
        local cameraFocus = startCFrame + startCFrame:vectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))

        --Set camera CFrame. I've played around with numbers and this seems to be where I should add the cutoff to make the camera move forward
        Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p) * CFrame.new(0, 0, -cutoff)
    end
end)

It seems correct, but when testing, the camera jitters back and forth between the CFrame before adding the cutoff, and after.

Any ideas?

Answer this question