So Im currently making a turret for a spaceship(1), I want the turret(2) to only spin on the y-axis to look at its target(3).
Everything works so far but the turret(2) has to spin along with the spaceship(1) when it turns or rotates.
When the spaceship(1) rotates on the x-axis the turret(1) doesnt continue looking at its target and instead has some kind of offset applied. I don’t understand why its doing this especially when ive set the turrets(2) X-axis rotation to its parents(1).
The code ive used is below
public Vector3 lookPos;
public Quaternion lookRot;
public float euler;
public Quaternion rotation;
lookPos = target.transform.position - this.transform.position;
lookRot = Quaternion.LookRotation(lookPos, Vector3.forward);
euler = lookRot.eulerAngles.y;
rotation = Quaternion.Euler(0, euler, 0);
this.transform.rotation = rotation;
this.transform.localEulerAngles = new Vector3(
this.transform.parent.transform.localEulerAngles.x,
this.transform.localEulerAngles.y,
this.transform.parent.transform.localEulerAngles.z);