# Making a 3D turret in unity, but having trouble with rotation

Hi there, I am making a turret in unity 3D. I have already made everything and it works: Shooting logic, Variables, etc. But the Rotation isn’t working if I put the turret on an angle. Basically the turret has 3 parts for rotation, The base, and then Pitch and Yaw for rotation. It’s the pitch and yaw that won’t rotate if it put the base on an angle. Here is the code for rotation:

``````public void RotateYaw()
{
if (Yaw != null && Target != null)
{
//float XOffset = Mother.transform.rotation.eulerAngles.x;

Vector3 targetDirection = Target.transform.position - Yaw.transform.position;
Quaternion targetRotation = Quaternion.LookRotation(targetDirection);

Vector3 currentEulerAngles = Yaw.transform.rotation.eulerAngles;
targetRotation.eulerAngles = new Vector3(Mother.transform.rotation.eulerAngles.x, targetRotation.eulerAngles.y + Mother.transform.rotation.eulerAngles.y, targetRotation.eulerAngles.z);

Yaw.transform.rotation = Quaternion.RotateTowards(Yaw.transform.rotation, targetRotation, Rotationspeed * Time.deltaTime);
}
}

public void RotatePitch()
{
if (Pitch != null)
{
Vector3 targetDirection = Target.transform.position - Pitch.transform.position;
Quaternion targetRotation = Quaternion.LookRotation(targetDirection);

// Adjust the target rotation by the yaw rotation
Quaternion yawRotation = Quaternion.Euler(0f, Yaw.transform.rotation.eulerAngles.y, 0f);
Quaternion adjustedRotation = yawRotation * targetRotation;

Quaternion pitchRotation = Quaternion.Euler(adjustedRotation.eulerAngles.x, 0f, adjustedRotation.eulerAngles.z);

Pitch.transform.localRotation = Quaternion.RotateTowards(Pitch.transform.localRotation, pitchRotation, Rotationspeed * Time.deltaTime);
}
}
``````

And here is a picture of the issue:

Please help I have been stuck on this for weeks.

PS: apologies for the bizarre variable names.