Turret Rotation restricted by a set number of degrees

Hi everyone. I am kinda getting frustrated with this code and wondered if any of you guys had a better idea on how to tackle the problem. Basicaly I have a turret attached to a ship, the turret can only rotate around its up axis(the heading of the cannon) and along its right axis (the “elevation” of the cannon). To keep the target on sight is rather easy, the problem is that I need to restrict the target rotation; so that the turret can rotate to target enemies on “the other side” of the ship(so to speak) So basicaly this keeps the target on sights:

Quaternion wantedRotation = Quaternion.LookRotation(targetTransform.position - thisTransform.position, targetTransform.up);
if (debug) Debug.Log("Local rotation: " + wantedRotation.eulerAngles);
if ((wantedRotation.eulerAngles.x >= 270  wantedRotation.eulerAngles.y >= 180  wantedRotation.eulerAngles.z >= 270) || 
(wantedRotation.eulerAngles.x >= 270  wantedRotation.eulerAngles.y <= 180  wantedRotation.eulerAngles.z <= 90) ) 
			transform.rotation = Quaternion.Slerp(transform.rotation, wantedRotation, turningSpeed);

The first part calculates the rotation needed to target the enemy, the second part is meant to restrict the rotation to be applied; only if the enemy can actually be targeted. The problem with this is that the Euler angles depend of the orientation of the ship, thus these are not always valid and I obviously don’t know how to get these dynamically to check if the rotation is valid or not. But really the optimum solution would be to cap the rotation to the one that gets closer to the target, so that if the ship moves; the turret will be able to target the enemy much quicker. In any case and help with this would be greatly appreciated guys.

Hey everybody. I just wanted to say that I finally solved this by casting a ray to the target, along its’ relative direction to the turret; if it hits anything but the target, then the rotation is not carried away. It’s definitely more demanding that what I wanted but it works for now. Hopefully someone will find this useful.