- Home /
Flight Movement with Banking
I am attempting to make an airplane automatically fly around a series of points in space. Right now the movement is locked to a plane along the X/Z axis (Y never changes) but ideally I would like to keep the option for full 3D flight open. The airplane constantly moves forward, while rotating around the Y axis to face its target, causing a little bit of an arc as it moves towards its target.
So far it is working well, but I am not sure how to add banking to the rotation calculations. As the plane makes sharp turns I want the plane to rotate around its forward axis.
I am just using the built in Quaternion.LookRotation function. I've tried setting the Z-rotation directly after the rotation towards the target is complete, but it seems to cause problems with the movement towards the target, and sends the plan off in undesired directions.
// Update is called once per frame
void Update ()
{
// Get the current target.
Transform currentTarget = targets[currentTargetIndex];
// Get a vector from the current position to the target.
// This is the vector we want to move our forward vector towards.
Vector3 lookDirection = currentTarget.position - transform.position;
// Calculate the rotation needed for this target look vector.
Quaternion rot = Quaternion.LookRotation(lookDirection.normalized);
// Rotate towards that target rotation based on the turn speed.
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * turnSpeed);
// With the rotation complete, just move forward.
transform.Translate(new Vector3(0,0,moveSpeed) * Time.deltaTime);
}
Let me know if there is anymore information I can add.
Answer by ThermalFusion · Sep 10, 2012 at 07:34 AM
Try:
float bank = 30f;
Quaternion rot = Quaternion.LookRotation(lookDirection.normalized) * Quaternion.AngleAxis(bank, Vector3.forward);
This seems to have the same issues as my original attempts. The rotation around the forward vector is not kept separate from the rotation around the up. The result is that the next frame Quaternion.LookRotation tries to remove the forward-axis rotation we added in the previous frame. The result is some awkward loop-d-loops. At least I think that's the cause.
Don't forget to mark an answer as correct so that other people know that the question is resolved. :)
You are right, this does introduce some movement in the y axis aswell. This is probably because the shortest rotation towards the target rotation is not around the y axis if you introduce some banking. There might be some magic to get this working in the xz plane, but it is beyond me. Here's another go I had on it:
void Update ()
{
Transform currentTarget = targets[currentTargetIndex];
Vector3 lookDirection = currentTarget.position - transform.position;
Vector3 normalizedLookDirection = lookDirection.normalized;
Debug.DrawRay(transform.position, lookDirection, Color.blue);
Debug.DrawRay(transform.position, transform.right, new Color(0.5f - (Vector3.Dot(transform.right, normalizedLookDirection)*0.5f), 0.5f ); (Vector3.Dot(transform.right, normalizedLookDirection)*0.5f), 0f));
float bank = maxBank * -Vector3.Dot(transform.right, normalizedLookDirection);
Quaternion rot = Quaternion.LookRotation(normalizedLookDirection) * Quaternion.AngleAxis(bank, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, Time.deltaTime * turnSpeed) ;
transform.Translate(new Vector3(0,0,moveSpeed) * Time.deltaTime);
}
But as stated, this does make the object start moving in the y axis aswell.
So I had another go at it, this time using a sub object to apply the banking to, it's a bit of a hack but worked great for me. You will need a child below the plane that you apply the banking to, Transform banker in the example:
public Transform[] targets;
public int currentTargetIndex = 0;
public float turnSpeed = 10f;
public float maxBank = 30f;
public float moveSpeed = 2f;
public Transform banker; // Child of this transform
void Update ()
{
Transform currentTarget = targets[currentTargetIndex];
Vector3 lookDirection = currentTarget.position - transform.position;
Vector3 normalizedLookDirection = lookDirection.normalized;
float bank = maxBank * -Vector3.Dot(transform.right, normalizedLookDirection);
Quaternion rot = Quaternion.LookRotation(normalizedLookDirection);
banker.localRotation = Quaternion.AngleAxis(bank, Vector3.forward);
transform.rotation = Quaternion.RotateTowards(transform.rotation, rot, Time.deltaTime * turnSpeed);
transform.Translate(new Vector3(0,0,moveSpeed) * Time.deltaTime);
}
This should keep the object on the xz plane while having banking aswell.
(Had to edit a lot to make the formatting work properly)
Great idea using parenting to isolate the rotation. That worked great thanks!
I've added a full class implementation to the thread but am just waiting for mod approval.
Answer by sandeepsmartest · Nov 10, 2012 at 02:23 PM
hey its really great bro.i too have the same issue i.e., me flight is turning in a curved way bt its not banking during turnings.actually i achieved banking and curved turning of the flight separately but i dont know how to integrate it together,below code makes the flight to bank: float fullbank = 40; float h = input.getaxis('horzontal") plane1.transform.rotation = Quaternion.Slerp(plane1.transform.rotation,Quaternion.AngleAxis(fullBank,Vector3.forward -h),Time.deltaTime 1.5f); but i want to bank my flight during turnings.plz help me .
Hi There.
Please don't post comments as answers. Post comments by clicking the [add new comment] button, a window then open for you to type in. Answer fields are for answers only, as this is a knowledge base.
You can convert this answer to a comment (or just edit your original question), you'll also get a better chance of getting an actual answer if the main list shows none or one answer in blue =]