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

Moving parts are glitchy with my custom camera?

Asked by
Muoshuu 580 Moderation Voter
9 years ago

I was making a custom camera, and I got everything to work fine, though when I moved, my character started to rapidly change positions...Any way I can fix this?

Code:

local Player=game.Players.LocalPlayer
repeat wait() until workspace:FindFirstChild(Player.Name.."_Body")
local Body=workspace:FindFirstChild(Player.Name.."_Body")
local Character=Player.Character
local Camera=workspace.CurrentCamera
local Mouse=Player:GetMouse()
Character:WaitForChild("Humanoid").AutoRotate=false
Character:WaitForChild("HumanoidRootPart")
local Settings={
    InvertMouse=false,
    Sensitivity=.1, --0 to 2
}
local Prev={X=Mouse.X,Y=Mouse.Y}

function Update()
    local NewX=Mouse.X
    local NewY=Mouse.Y
    local NewCF=Camera.CoordinateFrame
    if NewX>Prev.X then --Turn Right
        NewCF=NewCF*CFrame.Angles(0,(math.rad(-math.min(5,math.abs(NewX-Prev.X))))*Settings.Sensitivity,0)
    elseif NewX<Prev.X then --Turn Left
        NewCF=NewCF*CFrame.Angles(0,math.rad(math.min(5,math.abs(NewX-Prev.X)))*Settings.Sensitivity,0)
    end
    if NewY<Prev.Y then --Look Up
        NewCF=NewCF*CFrame.Angles(math.rad(math.min(5,math.abs(NewY-Prev.Y)))*Settings.Sensitivity,0,0)
    elseif NewY>Prev.Y then --Look Down
        NewCF=NewCF*CFrame.Angles(math.rad(-math.min(5,math.abs(NewY-Prev.Y)))*Settings.Sensitivity,0,0)
    end
    local a,b,c,d,e,f,g,h,i,j,k,l=NewCF:components()
    a,b,c=0,0,0
    Camera.CoordinateFrame=Body.Head.CFrame*CFrame.new(a,b,c,d,e,f,g,h,i,j,k,l)
    Prev.X=NewX
    Prev.Y=NewY
end
coroutine.wrap(function()
    while true do
        game:GetService("RunService").RenderStepped:wait()
        Camera.CameraType="Scriptable"
        Update()
    end
end)()

Answer this question