How do I make atan2 not rely on the way a part is facing

I am making a turret and it works fine when it is facing towards Positive X, but when I rotate the model the turret no longer rotates right. I am assuming that this is because atan2 gets the angle from the positive X axis to the point. Is there a way to redefine which way the X axis is pointing, or is there another way to get the angle that isn’t reliant on the direction the model is facing.

Code for angling

	Mouse.TargetFilter = script.Turret.Value

	local MouseTargetPos = Mouse.Hit.Position
	
	local OriginPointYaw = Vector2.new(script.Turret.Value.YawAxis.Position.X, script.Turret.Value.YawAxis.Position.Z)
	local TargetPointYaw = Vector2.new(MouseTargetPos.X, MouseTargetPos.Z)
	local AngleYaw = math.atan2((TargetPointYaw.Y-OriginPointYaw.Y), (TargetPointYaw.X-OriginPointYaw.X))

	local PitchDirection = MouseTargetPos - script.Turret.Value.PitchAimingBrick.Position
	local AnglePitch = math.asin(PitchDirection.Y / PitchDirection.Magnitude)

	AngleYaw = math.deg(AngleYaw)
	AnglePitch = math.deg(AnglePitch)
	AngleYaw = -AngleYaw
1 Like

Yaw should be (-Z, X) (and un-negate it in the last line), while you have (Y, X)

I solved it. I have no idea how I didn’t think of this but I just subtract the required amount of degrees from the final angle.