Hi, Im writing a simple tank game, It has a tank with a turret and a cannon attached and a hilly terrain.
The problem is that when the tank is at an angle the script doesnt rotate the turret and the cannon correctly. The turret house should only rotate on Z and the cannon attached on X. I think the problem is that my script doesnt take into account the new angle the tanks is in.
Can anybody help with this problem?
Thanks.
The following script controls the turret.
var TargetLoc : Vector3;
var targetObject : GameObject;
var RotateSpeed = 6.0;
function Update ()
{
if(targetObject !=null )
TargetLoc= targetObject.transform.position;
// Rotate towards target
var TankRotation = transform.rotation;
var targetRotation = Quaternion.LookRotation (TargetLoc - transform.position);
transform.rotation = Quaternion.Slerp(TankRotation, targetRotation, Time.deltaTime * RotateSpeed);
transform.localRotation.y = 0;
transform.localRotation.x = 0;
var targetRotation2 = Quaternion.LookRotation (TargetLoc - transform.Find("TurretHouse/Cannon").position);
transform.Find("TurretHouse/Cannon").rotation = Quaternion.Slerp(transform.Find("TurretHouse/Cannon").rotation, targetRotation2, Time.deltaTime * RotateSpeed);
transform.Find("TurretHouse/Cannon").localRotation.z =0;
transform.Find("TurretHouse/Cannon").localRotation.y =0;
}