Turret rotation(JS)

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:

transform.LookAt(Vector3(Target.position.x, transform.position.y, Target.position.z));

can anyone help.

Put this script on the Turret, and for the barrel use a simple transform.LookAt, but put the pivot at the beginning of the barrel.

var target : Transform;

function Update () {
	
	// Rotate towards target    
	var targetPoint = target.position;
	targetPoint.y = transform.position.y;
	
	var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
	transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
}

If you go to this answer here:

…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.

Use the first answer in this thread in order to clamp your angle.

limit local rotation

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).