Hello! I am having trouble with making a custom camera script. The problem that I have ran into is when you rotate the camera on the Z axis the camera doesn’t move with the mouse. For example, if you where to rotate 90 degrees on the Z axis and move the mouse up, the camera would move to the right. I have tried stuff with CFrame.fromMatrix
, but I could not get it to work properly.
Here is my camera script:
local runService = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local camera = workspace.CurrentCamera
local character = workspace:WaitForChild("character")
local dx, dy = 0, 0
local sensitivity = 0.1
local spawned = false
spawned = true
camera.CameraType = Enum.CameraType.Scriptable
uis.MouseBehavior = Enum.MouseBehavior.LockCenter
runService.RenderStepped:connect(function()
local delta = uis:GetMouseDelta()
dx += delta.Y * sensitivity
dy += delta.X * sensitivity
camera.CFrame = CFrame.new(character.Position) * CFrame.fromOrientation(-math.rad(dx), -math.rad(dy), math.rad(character.Orientation.Z))
end)
Thank you!
Edit: I need it to work for any Z angle, not just angles of 90 or 45.