hypotenuse_angle = atan2((target_distance-gun_offset.z), -gun_offset.x)
unit_vector.x = cos(hypotenuse_angle)
unit_vector.z = sin(hypotenuse_angle)
EDIT: Corrected signs.
Note that atan2 is a c function which takes the y value (target_distance-gun_offset.z) over the x value (-gun_offset.z) like a normal arctan function, but returns the angle in the proper quadrant. IF you do this with a normal calculator's arctan function, you will need to take the absolute value of y/x before giving it to the function and correct them manually. (eg if gun_offset.z is positive, your answer will be 180-angle, otherwise it will just be angle). Otherwise you'll get values for a target behind and to the right of the ship when you calculate values forward and to the left of the ship.