How would I stop the camera from rotating on the z axis?

I want the camera to only rotate on the y and x axis

Video of camera’s z axis being changed by the y and x axis

I have tried just taking the camera’s z axis and feeding it in as a negative, but the z axis still rotates slightly, not giving the intended effect.

Cannot think of any solutions, if anyone could provide sources / assistance with this matter it would be great. Thanks!

Code that has problems is below:

local function rotateCamera()
	local mouse = plr:GetMouse()
	local lastPosition = Vector2.new(mouse.X, mouse.Y)
	local sensitivity = 0.005

	while UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) do
		local currentPosition = Vector2.new(mouse.X, mouse.Y)
		local delta = currentPosition - lastPosition

		local rotation = CFrame.Angles((delta * sensitivity).Y, (delta * sensitivity).X, 0)

		cam.CFrame = cam.CFrame * rotation

		lastPosition = currentPosition
		wait()
	end
end

Copy paste this code into a local script in starter player to test it out for yourself:
Note the code above is the problem, but you can check if you want.

local UIS = game:GetService("UserInputService")
local plr_Service = game:GetService("Players")
local plr = plr_Service.LocalPlayer

plr.CameraMode = Enum.CameraMode.LockFirstPerson
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter

local cam = game.Workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable


local function rotateCamera()
	local mouse = plr:GetMouse()
	local lastPosition = Vector2.new(mouse.X, mouse.Y)
	local sensitivity = 0.005

	while UIS:IsMouseButtonPressed(Enum.UserInputType.MouseButton2) do
		local currentPosition = Vector2.new(mouse.X, mouse.Y)
		local delta = currentPosition - lastPosition

		local rotation = CFrame.Angles((delta * sensitivity).Y, (delta * sensitivity).X, 0)

		cam.CFrame = cam.CFrame * rotation

		lastPosition = currentPosition
		wait()
	end
end

plr:GetMouse().Button2Down:Connect(function()

	rotateCamera()

end)

UIS.InputBegan:Connect(function(input)

	local startCFrame = cam.CFrame

	local start = os.clock()

	if input.KeyCode == Enum.KeyCode.W then

		local moveCam = true

		local connected = UIS.InputEnded:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.W then
				moveCam = false
			end
		end)

		while moveCam == true do
			cam.CFrame += cam.CFrame.LookVector * (os.clock() - start)
			task.wait()
		end

		connected:Disconnect()

	elseif input.KeyCode == Enum.KeyCode.A then

		local moveCam = true

		local connected = UIS.InputEnded:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.A then
				moveCam = false
			end
		end)

		while moveCam == true do
			cam.CFrame -= cam.CFrame.RightVector * (os.clock() - start)
			task.wait()
		end

		connected:Disconnect()

	elseif input.KeyCode == Enum.KeyCode.S then

		local moveCam = true

		local connected = UIS.InputEnded:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.S then
				moveCam = false
			end
		end)

		while moveCam == true do
			cam.CFrame -= cam.CFrame.LookVector * (os.clock() - start)
			task.wait()
		end

		connected:Disconnect()

	elseif input.KeyCode == Enum.KeyCode.D then

		local moveCam = true

		local connected = UIS.InputEnded:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.D then
				moveCam = false
			end
		end)

		while moveCam == true do
			cam.CFrame += cam.CFrame.RightVector * (os.clock() - start)
			task.wait()
		end

		connected:Disconnect()

	elseif input.KeyCode == Enum.KeyCode.E then

		local moveCam = true

		local connected = UIS.InputEnded:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.E then
				moveCam = false
			end
		end)

		while moveCam == true do
			cam.CFrame += cam.CFrame.UpVector * (os.clock() - start)
			task.wait()
		end

		connected:Disconnect()
		
	elseif input.KeyCode == Enum.KeyCode.Q then

		local moveCam = true

		local connected = UIS.InputEnded:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.Q then
				moveCam = false
			end
		end)

		while moveCam == true do
			cam.CFrame -= cam.CFrame.UpVector * (os.clock() - start)
			task.wait()
		end

		connected:Disconnect()
		
	end
end)
2 Likes
-- Assuming cam is your Camera object
local cam = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local sensitivity = 0.1

mouse.Button2Down:Connect(function()
    local lastPosition = Vector2.new(mouse.X, mouse.Y)
    
    local function rotateCamera()
        while mouse.Button2Down do
            local currentPosition = Vector2.new(mouse.X, mouse.Y)
            local delta = currentPosition - lastPosition

            local rotation = CFrame.Angles(0, -delta.x * sensitivity, 0)
            cam.CFrame = cam.CFrame * rotation

            lastPosition = currentPosition
            wait()
        end
    end

    rotateCamera()
end)

mouse.Button2Up:Connect(function()
    -- Stop rotating when right mouse button is released
end)

1 Like

that does not solve my problem, that just rotates the camera on the y axis, not the x axis. I want to rotate my camera on both the y and x axis as stated in the post.

-- Assuming cam is your Camera object
local cam = game.Workspace.CurrentCamera
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local sensitivity = 0.1

local function rotateCamera()
    local lastPosition = Vector2.new(mouse.X, mouse.Y)
    
    while mouse.Button2Down do
        local currentPosition = Vector2.new(mouse.X, mouse.Y)
        local delta = currentPosition - lastPosition

        -- Rotate on both Y and X axes
        local rotationY = CFrame.Angles(0, -delta.x * sensitivity, 0)
        local rotationX = CFrame.Angles(-delta.y * sensitivity, 0, 0)

        -- Apply rotations sequentially
        cam.CFrame = cam.CFrame * rotationY * rotationX

        lastPosition = currentPosition
        wait()
    end
end

mouse.Button2Down:Connect(rotateCamera)

mouse.Button2Up:Connect(function()
    -- Stop rotating when right mouse button is released
end)

1 Like