- Home /
Trouble orienting a GO using transform.up
I am coding the AI for my airplane-like enemies. Due to their nature as aircraft, they have a constant forward speed and would change heading by rotating their up vector in the direction of the enemy, then pitching up until the nose is pointing at the enemy. I did this using the game logic that follows:
First, I found the target position with respect to the plane (InverseTransformPoint), then I ignored the z component to collapse the problem to a two dimensional one. Then I used Atan2 to get the rotation angle from the forward vector of the plane to the target. Finally, I send it to the aircraft script and LERPed the angle. The aircraft "rolls" in its FixedUpdate. The script looks like:
In the AI choice switch statement:
case 3:
objective = AIObjective.attack;
primaryTarget = SelectNearbyAllies();
desiredPitch = -.5;
if (!primaryTarget) {
objective = AIObjective.straight;
desiredPitch = 0;
}
break;
Later, set the target as the enemy GO:
case AIObjective.attack:
targetPoint = primaryTarget.transform.position;
break;
Then find the proper angle:
var tempVec: Vector3= transform.InverseTransformPoint(targetPoint);
desiredRoll = (Mathf.Atan2(tempVec.y,tempVec.x) * Mathf.Rad2Deg)+90;
if (desiredRoll < 0) desiredRoll +=360;
if (tempVec.x*tempVec.x + tempVec.y*tempVec.y < .001) desiredRoll = 0;
SendRoll(desiredRoll);
SendPitch(desiredPitch);
And in the aircraft script:
var newRoll = Mathf.MoveTowardsAngle(0, -roll - transform.eulerAngles.z, rollSpeed*Time.deltaTime);
if (newRoll != 0) transform.RotateAround(transform.position,transform.forward, newRoll);
if (pitch != 0) transform.RotateAround(transform.position,transform.right,pitch*Time.deltaTime);
Any advice would be appreciated. Please remember that I must send only the amount to be rolled and the necessary pitch to the aircraft script (without a serious rewrite), and that the aircraft can only pitch from transform.forward towards transform up (along that 2D plane). Thanks!
EDIT:
I've gotten close with the following:
if (!primaryTarget) return;
var tempVec: Vector3 = primaryTarget.transform.position - transform.position;
tempVec = tempVec.normalized;
var upVec: Vector3 = Vector3.Cross(transform.forward, tempVec);
upVec = upVec.normalized;
var combinedVec: Vector3 = Vector3.Cross(tempVec,upVec);
combinedVec = combinedVec.normalized;
Debug.DrawLine(transform.position, transform.position + tempVec *10, Color.blue);
Debug.DrawLine(transform.position, transform.position + upVec *10, Color.red);
Debug.DrawLine(transform.position, transform.position + combinedVec *10, Color.yellow);
var dR: float = Mathf.Atan2(combinedVec.z,combinedVec.y) * Mathf.Rad2Deg;
if (dR < 0) dR +=360;
dR = 360-dR;
Debug.Log(dR);
SendRoll(dR);
Now my problem is that I can't pitch towards the target... once I do the plane spins erratically or not at all... Once again, help would be appreciated!
Above I said that I needed help with just the roll amount per frame... but if anyone has a better way to reorient the plane along the transform.up and then pitch... I'm more than open to it!