Rotate an object to align with another along one axis?

I have a behavior script for a turret and want it to make it aim in a way so that the shaft only turns left and right to align with the target and the barrels only up and down to point at it, but i’m not exactly sure how to do so.

Here’s what i have so far (which only makes the turret spin wildy):

void LookAt (GameObject target)
    {
        Vector3 targetRelativePosition = target.transform.position - transform.position;

        float targetRotation = Mathf.Atan2 (targetRelativePosition.x, targetRelativePosition.y) * Mathf.Rad2Deg + shaft.transform.eulerAngles.y;
        shaft.transform.eulerAngles = Vector3.up * targetRotation;
    }

I use transform.lookat like this:

Vector3 lookPos = target.transfrom.position;
lookPos.y = transform.position.y;
transform.LookAt(lookPos);

I think you would use the transform.z for the barrel.
Probably have to slerp it so it moved slow.