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

Mouse sensitivity help?

Asked by
Spectrobz 140
9 years ago

Hello, I'm currently working on guns and I need a smooth mouse sensitivity script, I've been looking in the free models and found this

function RotateCamera(x, y)
    Camera.CoordinateFrame = CFrame.new(Camera.Focus.p) * (Camera.CoordinateFrame - Camera.CoordinateFrame.p) * CFrame.Angles(x, y, 0) * CFrame.new(0, 0, (Camera.CoordinateFrame.p - Camera.Focus.p).magnitude)
end

function GetAngles(cf)
    local lv = cf.lookVector
    return -math.asin(lv.y), math.atan2(lv.x, -lv.z)
end

local LastCF = Camera.CoordinateFrame

function UpdateSensitivity()
    if _G.MouseSensitivity ~= 1 then -- no need to do this if it's 1
        local x, y = GetAngles(LastCF:toObjectSpace(Camera.CoordinateFrame))
        Camera.CoordinateFrame = LastCF
        RotateCamera(x * -(_G.MouseSensitivity + _G.MouseSensitivityOffset), y * -(_G.MouseSensitivity + _G.MouseSensitivityOffset))
        LastCF = Camera.CoordinateFrame
    end
end

Mouse.Move:connect(function()
    UpdateSensitivity()
end)

Mouse.Idle:connect(function()
    LastCF = Camera.CoordinateFrame
end)

But the camera rotation is not smooth and a bit shaking. I'm trying to use render stepped to make the rotation smooth, but yet I dont find a way to get it works, this is what I tried so far, and it doesn't work (camera gets stuck to its first position)

local LastCF = Camera.CoordinateFrame

function UpdateSensitivity()
    if _G.MouseSensitivity ~= 1 then
        local x, y = GetAngles(LastCF:toObjectSpace(Camera.CoordinateFrame))
        Camera.CoordinateFrame = LastCF
        RotateCamera(x * -(_G.MouseSensitivity + _G.MouseSensitivityOffset), y * -(_G.MouseSensitivity + _G.MouseSensitivityOffset))
    end
end

game:GetService("RunService").RenderStepped:connect(function()
    if Camera.CoordinateFrame ~= LastCF then
        UpdateSensitivity()
    end
    LastCF = Camera.CoordinateFrame
end)

It breaks because it uses the last saved position of the camera, yet it should only use the old rotation, apply sensitivity and apply it to the new position. Does anyone know how to fix it ?

Thanks for helping me asap.

Answer this question