Camera CFrame not Tilting on Z Axis

I want to make it so that a function will make the Camera tilt to the right. It works for other axis, but not Z. I am not sure why, but this is very confusing. Here’s my code:

function TiltCameraRight()
    local Camera = game.Workspace.CurrentCamera
    Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, math.rad(30))
end

The default camera scripts reset roll

You can overwrite it by using RenderStepped

local currentCFrame = workspace.CurrentCamera.CFrame

local roll = math.rad(30)
local rollCFrame = CFrame.Angles(0, 0, roll)
workspace.CurrentCamera.CFrame = currentCFrame * rollCFrame
print("Roll")
wait(5)
print("Continuos Roll")

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function(dt)
	local currentCFrame = workspace.CurrentCamera.CFrame
	workspace.CurrentCamera.CFrame = currentCFrame * rollCFrame
end)

The line of code causing this issue is within the default camera script with CFrame.new() commented below
image

function BaseCamera:CalculateNewLookCFrameFromArg(suppliedLookVector: Vector3?, rotateInput: Vector2): CFrame
	local currLookVector: Vector3 = suppliedLookVector or self:GetCameraLookVector()
	local currPitchAngle = math.asin(currLookVector.Y)
	local yTheta = math.clamp(rotateInput.Y, -MAX_Y + currPitchAngle, -MIN_Y + currPitchAngle)
	local constrainedRotateInput = Vector2.new(rotateInput.X, yTheta)
--Uses CFrame.new() which has zero Z axis roll
	local startCFrame = CFrame.new(ZERO_VECTOR3, currLookVector) -- This resets the Z roll
	local newLookCFrame = CFrame.Angles(0, -constrainedRotateInput.X, 0) * startCFrame * CFrame.Angles(-constrainedRotateInput.Y,0,0)
	return newLookCFrame
end

Thanks, but how would I make it so that the roll can be disabled? I don’t want the roll to be permanent.

No problem,

To disable it simply disconnect the connection like so:

local currentCFrame = workspace.CurrentCamera.CFrame

local roll = math.rad(30)
local rollCFrame = CFrame.Angles(0, 0, roll)
workspace.CurrentCamera.CFrame = currentCFrame * rollCFrame
print("Roll")
wait(5)
print("Continuos Roll")

local RunService = game:GetService("RunService")

local rollConnection = RunService.RenderStepped:Connect(function(dt)
	local currentCFrame = workspace.CurrentCamera.CFrame
	workspace.CurrentCamera.CFrame = currentCFrame * rollCFrame
end)

rollConnection:Disconnect()

Or you can set the rollCFrame to zero

workspace.CurrentCamera.CFrame = currentCFrame * rollCFrame
print("Roll")
wait(5)
print("Continuos Roll")

local RunService = game:GetService("RunService")

local roll = math.rad(30)

local rollConnection = RunService.RenderStepped:Connect(function(dt)
	local currentCFrame = workspace.CurrentCamera.CFrame
local rollCFrame = CFrame.Angles(0, 0, roll)
	workspace.CurrentCamera.CFrame = currentCFrame * rollCFrame
end)

task.wait(5)
roll = 0 --set roll to zero
1 Like

This is a little bit extra, but do you know how I would offset the camera? I want to make the camera move to the side a bit as well. Thanks for your help.

Can you clarify further and have you searched other topics?

Move to the side is pretty vague. You should break it down further such as left side or right side, then break it down even further untill you get into programming terms such as rotate +30 degrees on the camera local y axis.

I believe you are looking for this though just found it, offset without overwriting the original camera CFrame:

2 Likes