hi, over the last few days I have been trying to make a turret which points towards a target, the turret is in two pieces; The middle section which turns 360 degrees, and the barrel which turns 70 degrees, here is a picture:

[35696-turret+rotation.png|35696]

But what I get is a turret rotating in the wrong direction no matter what I do.

Here is the turret rotation script, this does not affect the barrel, but I will need to change it for the barrel script:

#pragma strict
var Target : Transform;
var v3Rotate = Vector3.zero;
var min = -100;
var max = 100;
var rotateSpeed : float = 45.0;
private var Update_Timer : float = 3;
function Start (){
transform.localEulerAngles = v3Rotate;
}
function Update () {
transform.LookAt(Vector3(Target.position.x, transform.position.y, Target.position.z));
v3Rotate.x = Mathf.Clamp(v3Rotate.x, min, max);
}

I have changed the values at the bottom on this line:

â€¦and open up the comment section of my answer, you will find a script that does what you want including limiting the rotation. Note the geometry has to be setup correctly for this code to work.

You can use psycocrusherâ€™s code to smoothly rotate both base and barrel.

function AimTurret(aimpoint:Vector3) {
//Get target rotation, assuming turret and cannon have different pivot points
var targetRotationX = Quaternion.LookRotation(aimpoint-turret.position);
var targetRotationY = Quaternion.LookRotation(aimpoint-cannon.position);
//Rotate turret 40 degrees per second and cannon 10 degrees per second towards respective target rotations
turret.rotation = Quaternion.RotateTowards(turret.rotation,targetRotationX,40*Time.deltaTime);
cannon.rotation = Quaternion.RotateTowards(cannon.rotation,targetRotationY,10*Time.deltaTime);
//Constrain rotations to respective axes, clamp cannon between 0-70 degrees
turret.localEulerAngles = Vector3(0,turret.localEulerAngles.y,0);
cannon.localEulerAngles = Vector3(ClampAngle(cannon.localEulerAngles.x,-70,0),0,0);
}

Modify speed and min-max values to your liking, and just call it in your Update like AimTurret(target.position).