Roblox camera automatically turns in Z axis (fixed)

So,hello there!

I have tried to make a custom first person camera but its weirdly turning on the z axis by itself.


Heres the code :

wait()
local cam = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local char = plr.Character
local head = char:FindFirstChild("Head")
local angles = CFrame.Angles(0,0,0)
local cf = CFrame.new()
local mouse = plr:GetMouse()
local mocx,mocy = mouse.X,mouse.Y
local mcx,mcy = mouse.X,mouse.Y
local sensitivity = 1
--how many pixels your cursor needs to go before detection

cam.CameraType = Enum.CameraType.Scriptable



mouse.Move:Connect(function()
	mcx,mcy = mouse.X,mouse.Y
	if mcx ~= mocx or mcy ~= mocy then
		local camy = mocy - mcy
		local camx = mocx - mcx
		if math.abs(camy) < sensitivity then
			camy = 0
		end
		if math.abs(camx) < sensitivity then
			camx = 0
		end
		angles = CFrame.Angles(math.rad(camy),math.rad(camx),math.rad(0))
		angles = cam.CFrame.Rotation * angles
		cf = CFrame.new(head.CFrame.Position) * angles
		cam.CFrame = cf
	end
	mocx,mocy = mouse.X,mouse.Y
end)

heres the result

robloxapp-20241014-1945042.wmv (5.6 MB)


The solutions ive tried so far :

Try some other posts i found on the devforum

Try fixing it myself


Because im not so good at roblox lua,i couldnt fix it but maybe someone will help me?

Try fixing the auto Z-axis camera thing , thanks!

-me

hello everyone,im happy to finally say that ive fixed this.

this is the code that fixes it:

function turnCameraXY(x,y,cam)
	cam.CFrame = cam.CFrame:Lerp(CFrame.new(head.Position) * cam.CFrame.Rotation * CFrame.Angles(x,y,0), 0.7)
	local _,_,z2 = cam.CFrame:ToOrientation()
	cam.CFrame *= CFrame.fromEulerAnglesXYZ(0,0,-z2)
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.