I’m trying to create some scripts that will attach to a tank model, so I made a general script that will face an axis to a position in world space, but the math isn’t mine, I copy pasted off of somebody elses tank controller script. The script works fine if I’m only using one axis, but messes up if you try to use more than one. Could you help me understand what I would need to do to have the script work on more than one axis?
// You can only use one axis at a time, I need to fix this eventually
#pragma strict
var target : Transform ;
var LimitToY : boolean = false;
var LimitToX : boolean = false;
var LimitToZ : boolean = false;
function Update () {
var targetVector : Vector3 = target.transform.position - transform.position;
var localHeading : Vector3 = transform.InverseTransformDirection(targetVector);
if (LimitToY){
var requiredYaw : float = Mathf.Rad2Deg * Mathf.Atan2(localHeading.x, localHeading.z);
transform.Rotate(Vector3.up, requiredYaw, Space.Self);
}
if (LimitToX){
var requiredPitch : float = Mathf.Rad2Deg * Mathf.Atan2(localHeading.z, localHeading.y);
transform.Rotate(Vector3.right, requiredPitch, Space.Self);
}
if (LimitToZ){
var requiredRoll : float = Mathf.Rad2Deg * Mathf.Atan2(localHeading.y, localHeading.x);
transform.Rotate(Vector3.forward, requiredRoll, Space.Self);
}
}