TopDown camera moving with mouse incorrectly

I’m trying to make a top-down camera that is over the player but moves in the direction of the mouse. However, it seems like the X and Y positions are swapped. Is there any way to fix this?

local downCameraCf =CFrame.Angles(math.rad(-90),0,0)
threshold = 5
runService.RenderStepped:Connect(function(dt)
	if character then
		local mousep = mouse.Hit.p
		local targetPos = (rootpart.Position +mousep) / 2;    
		local x = math.clamp(targetPos.X, -threshold + rootpart.Position.X, threshold + rootpart.Position.X)
		local z = math.clamp(targetPos.Z, -threshold + rootpart.Position.Z, threshold + rootpart.Position.Z)
		targetPos = CFrame.new(x,rootpart.Position.Y,z)
		camera.CFrame =  targetPos*downCameraCf+Vector3.new(0,25,0)
	end
end)

this is the visual example of what’s happening:
https://gyazo.com/b7f7a4f0102c509d805b29d233088e57
Edit: video format if the link isn’t working

I personally suck at CFrames but maybe try reversing the order of x and z.

CFrame.new(z, rootpart.Position.Y, x)

I tried, but it just offset the camera to the right sadly, since the spawn point is at X0 and Z50 I think

Turns out it’s due to the camera’s rotation.
local downCameraCf =CFrame.Angles(math.rad(-90),0,math.rad(-90))
Fixes the mouse position being offset from where the actual mouse is. However, now my camera is facing the right of where it is supposed to be. Is there any way to get the actual mouse position? I’ve tried both ScreenPointToRay and ViewportPointToRay, but they produce the same camera dependant result.

Edit: here’s what I mean
with rotation


without rotation

Fixed by setting the camera type to scriptable, but I lost invis cam sadly, so I’ll make a custom version of it.