Calculate the Y angle of Mouse

I am trying to script a tank. One of the things I am having a lot of trouble with is calculating how the Barrel of the tank will angle up and down depending on the position of your Mouse.

The Barrel uses a HingeConstraint, and TargetAngle to angle it, though I must state that the barrel is like reversed, so -10 TargetAngle means it is angled Upwards, and positive 10 TargetAngle means it is angled down, I don’t know whether if this just means to inverse the calculation or if it requires extra steps but I’d rather state it just in case.

My problem? The code I am using to determine the angle is working improperly. I’ve noticed that sometimes it can correctly determine angles but only in certain situations making it extremely unreliable outside of that.

local mousePosition = Mouse.Hit.Position
local ObjectVerticalOffset = (BarrelHinge.Parent.Attachment0.WorldCFrame):PointToWorldSpace(mousePosition)
local ObjectPitchAngle = math.atan(-ObjectVerticalOffset.Z/-ObjectVerticalOffset.Y)
ObjectPitchAngle = math.deg(ObjectPitchAngle)
BarrelHinge.TargetAngle = ObjectPitchAngle

Any help is appreciated.