Need help with lean system

Upon executing this script, it sets the camera back to it’s initial position and I don’t know how I’d fix it.

local userInputService = game:GetService('UserInputService')
local cam = workspace.CurrentCamera 

local right = 0
local left = 0
local function move()
    local LeanAmount = left + right
    local Lean = CFrame.fromAxisAngle(Vector3.new(0, 0, 1), LeanAmount)
    local tween = game:GetService('TweenService'):Create(cam, TweenInfo.new(0.3), {
        CFrame = cam.CFrame * Lean 
    })
    tween:Play()
end
userInputService.InputBegan:Connect(function(input, gpe)
    if not gpe then
        if input.KeyCode == Enum.KeyCode.Q then
            left = -0.2
            move()
        end
        if input.KeyCode == Enum.KeyCode.E then
            right = 0.2
            move()
        end
    end
end)

userInputService.InputEnded:Connect(function(input) 
    if input.KeyCode == Enum.KeyCode.Q then
        left = 0
        move()
    end
    if input.KeyCode == Enum.KeyCode.E then
        right = 0
        move()
    end
end)

Hey Gettuse,

You have probably looked into this if looking for a leaning camera effect but I suggest using this, it might help you out.

Camera:SetRoll

Hope this helps :slight_smile:

Hi Carl,

I’m unsure if using SetRoll works as I’ve just added it to my script and it seems that nothing occurs when I execute the script.

local userInputService = game:GetService('UserInputService')
local cam = workspace.CurrentCamera 

local right = 0
local left = 0
local function move()
	local LeanAmount = left + right
	--local Lean = CFrame.fromAxisAngle(Vector3.new(0, 0, 1), LeanAmount)
--	local tween = game:GetService('TweenService'):Create(cam, TweenInfo.new(0.3), {
--		CFrame = cam.CFrame * Lean 
--	})
--	tween:Play()
	--cam.CFrame = cam.CFrame * Lean 
	local currentCFrame = workspace.CurrentCamera.CFrame
	local rollCFrame = CFrame.Angles(0, 0, LeanAmount)
	workspace.CurrentCamera.CFrame = currentCFrame * rollCFrame
end
userInputService.InputBegan:Connect(function(input, gpe)
	if not gpe then
		if input.KeyCode == Enum.KeyCode.Q then
			left = -90.2
			move()
		end
		if input.KeyCode == Enum.KeyCode.E then
			right = 90.2
			move()
		end
	end
end)

userInputService.InputEnded:Connect(function(input) 
	if input.KeyCode == Enum.KeyCode.Q then
		left = 0
		move()
	end
	if input.KeyCode == Enum.KeyCode.E then
		right = 0
		move()
	end
end)

The Developer hub website you’ve linked stated that using Camera:SetRoll is an outdated method.

Update: Apparently the problem was that I wasn’t maintaining the offset, and roblox essentially takes back the wheel when I update the camera. So upon changing the move() function to a RunServer.RenderStepped function. The script works, however the tween doesn’t.