Tank turret not aiming to mouse (hinge constraint)

im using a hinge constraint to rotate the tank turret to the mouse and it works fine if you are on flat gound but if you go on a ramp it gets all messed up (see video)

code:

local rotateOrigin = chassis.Structure.TurretBase
local turretOrigin = chassis.Turret.GunRotateBase
local turretBase = motors.TurretBase
local turret = motors.Turret
if not rotateOrigin then return end

local offset = rotateOrigin.CFrame:PointToObjectSpace(mouse.Hit.Position)
local angle = math.atan2(-offset.X, -offset.Z)
if engine_enabled and turret_enabled and not repairing then
    turretBase.TargetAngle = math.deg(angle)
end

local goal = CFrame.lookAt(
    Vector3.new(),
    turretOrigin.CFrame:PointToObjectSpace(mouse.Hit.Position)
)

local y, x, z = goal:ToOrientation()
if engine_enabled and turret_enabled and not repairing then
    turret.TargetAngle = math.deg(y)
end

turretBase is the main part of the turret
turret is the gun of the turret
rotateOrigin is a part under the turretBase with the front face facing forward
turretOrigin is the part that has the turretBase hinge attached

switched to using cframe and motor6ds