Hi all,
I have a model of a gun turret, the turret will rotate horizontally to track the target, the gun (a child of the turret) will rotate up and down to track the vertical movement of the target, all sitting on a base, so only 3 primary components. In a sense , it is exactly the same as a tank turret rotating towards a target then aiming the gun up and down.
The body is a child object of a pivot (turret_pivot) .
The gun is a child object of another pivot (gun_pivot), this pivot is a child of turret_pivot.
This is so that everything turns horizontally when I rotate the turret_pivot horizontally, and then I rotate the gun_pivot vertically, leaving the body of the turret horizontal.
I thought this would be straight forward, but have encountered some strange issues; once the target moves to z=0 distance, the guns point directly upwards, and once z goes negative (target moves behind the turret), the turret continues to rotate forwards, exactly 180 degrees ie mirroring where it should be pointing.
var dir1 :Vector3 =(CurrentTarget.position-transform.position).normalized; //points at the target
var dir2 :Vector3 =(CurrentTarget.position-transform.position).normalized ;
dir1.y = 0; // keep only the horizontal direction
dir2.x=0;; //keep only the vertical direction
var rot1 = Quaternion.LookRotation(dir1); //for rotating the body and gun pivot
var rot2= Quaternion.LookRotation(dir2); //for rotating the gun, it will look up and down
turet_pivot.localRotation = Quaternion.Slerp(turet_pivot.localRotation, rot1, 25Time.deltaTime);
gun_pivot.localRotation = Quaternion.Slerp(gun_pivot.localRotation, rot2, 25Time.deltaTime);