Turret with rotation

I have a turret mounted on a spaceship. This spaceship can drift to any rotational angle. To lock-on an object, I need the (local) rotational X and Y to train the turret on the target. For example 0, 0 is dead ahead / default rotation for the turret. +X rotates the turret right, +Y rotates the turret up. The code that works only for dead level targeting is shown below:

Quaternion r = Quaternion.LookRotation(pos - transform.position);
Vector3 ea = r.eulerAngles;
return new Vector2((float)fixAngle(180+ea.y), (float)fixAngle(-ea.x));

This code tracks the target right on the dot. However I have been unable to find a way to track a target when the ship (along with the turret) is rotated. The Quaternion.LookRotation method completely overlooks the rotation of the ship (as expected). So I have been experimenting with mixing in transform.rotation of the turret into the r Quaternion to see if that would properly orient the angle, but so far I am stuck at guessing.

By the way, the method fixAngle constrains the input angle to the range 180, -180 (while keeping the true angle). The code is shown below:

double fixAngle(double angle)
{
    if (angle > 180){angle -= 360;}
    if (angle < -180){angle += 360;}
    return angle;
}

UPDATE:

I have been hacking to get this example working, however I have been consistently failing. So then it dawned on me to use dot products! With dot products, each direction can be isolated to a confined quantity. With these quantities, I can then use arc-tangent to solve for the angle (or rather the atan2 function). My code is currently:

Vector3 d;
double up, forward, right, x, y;
d = pos - transform.position;
up = Vector3.Dot(transform.up, d);
forward = Vector3.Dot(transform.right, d);
right = Vector3.Dot(-transform.forward, d);

x = Math.Atan2(right, forward);
y = Math.Atan2(up, forward);

return new Vector2((float)fixAngle(x * (180/3.1415)), (float)fixAngle(y * (180/3.1415)));

The code listed above is VERY close. It works for quite a large range of the angles. However, I can still get into an angle that it screws up. I am in the strong belief that I mixed up one (or more) of the function’s operational order. The logic behind getting this to work is sound (to the best of my knowledge), so I’ll just keep hammering it out.

FINAL UPDATE! (DONE):

Yes, it is now complete! My minor math mistake was y = Math.Atan2(up, forward); needs to be y = Math.Atan2(up, dist(forward, right)); where dist is a simple distance formula sqrt(a^2 + b^2). This change was needed because the hypotenuse was the new adjacent side for atan2! Simple fix, now turret has amazing accuracy!

Due to a number of factors, this can be a complicated problem to solve. If you have just a turret then you can project the point onto the plane. You will find a bit more info here:

http://answers.unity3d.com/questions/553180/turret-lookrotation-problem.html

If you have both a turret with a gun attached, you will find some source code and a link to a working package using this source here:

http://answers.unity3d.com/questions/562443/turret-slerp-rotation-and-clamping.html

Note you will have to expand out the comments, since the source and link to the package is contained in one of the later comments.