Ad
Log in to vote
0

Why is the camera spassing out whenever I try to rotate it?

Asked by 8 days ago
Edited 8 days ago

CODE:

repeat wait() until script.Parent:FindFirstChild("Humanoid")

local userInputService = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
local leanDirection = nil
local leanInc = 3
local defaultOffset = Vector3.new(0,0,0)
local eOffset = Vector3.new(leanInc,0,0)
local qOffset = Vector3.new(-leanInc,0,0)
local defaultCameraOffset = CFrame.Angles(0,math.rad(0),0)
local eCameraOffset = CFrame.Angles(0,math.rad(45),0)
local qCameraOffset = CFrame.Angles(0,math.rad(-45),0)
local tweening = false

local function tween(object, goal)
    local tweenInfo = TweenInfo.new(0.25, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut)
    game:GetService("TweenService"):Create(object, tweenInfo, goal):Play()
    tweening = true
    wait(0.25)
    tweening = false
end

local function updateCameraPOS()
    if leanDirection == nil then
        tween(script.Parent.Humanoid, {CameraOffset = defaultOffset}) 
    elseif leanDirection == "E" then
        tween(script.Parent.Humanoid, {CameraOffset = eOffset}) 
    elseif leanDirection == "Q" then
        tween(script.Parent.Humanoid, {CameraOffset = qOffset}) 
    end
end

userInputService.InputBegan:Connect(function(input, gameProcessed)
    if not gameProcessed then
        if input.KeyCode == Enum.KeyCode.E then
            leanDirection = "E"
        elseif input.KeyCode == Enum.KeyCode.Q then
            leanDirection = "Q"
        end
    end
    updateCameraPOS()
end)

userInputService.InputEnded:Connect(function(input, gameProcessed)
    if not gameProcessed then
        if input.KeyCode == Enum.KeyCode.E and leanDirection == "E" then
            leanDirection = nil
        elseif input.KeyCode == Enum.KeyCode.Q and leanDirection == "Q" then
            leanDirection = nil
        end
    end
    updateCameraPOS()
end)

game:GetService("RunService").RenderStepped:Connect(function()
    if not tweening then
        if leanDirection == nil then
            script.Parent.Humanoid.CameraOffset = defaultOffset
            camera.CFrame = CFrame.Angles(0, math.rad(0), 0)
        elseif leanDirection == "E" then
            script.Parent.Humanoid.CameraOffset = eOffset
            camera.CFrame = CFrame.Angles(0, math.rad(-45), 0)
        elseif leanDirection == "Q" then
            script.Parent.Humanoid.CameraOffset = qOffset
            camera.CFrame = camera.CFrame * CFrame.Angles(45, 0, 0)
        end
    end
end)

The camera spasses out, and if I remove the camera.CFrame lines, it works good. However, whenever you lean I want the camera to tilt too. Someone please help me, I've been stuck for days and I've posted this several time on the developer forum and still, nobody is helping me.

Thanks!

Answer this question