- Home /
Aircraft Banking Rotation Problem
Hi, I'm creating a simple aircraft control script, using rigidbody.AddTorque to rotate my aircraft when the user makes control inputs.
When the aircraft rolls to the left, I want it to also rotate around the world Y axis towards the left, to simulate the aircraft turning as a result of the bank angle.
The problem I'm having is that even though I'm only applying rigidbody.AddTorque around the Y axis, the resulting rotation causes the aircraft's nose to dip down and eventually turn upside-down. I need the aircraft to stay level (i.e not pitch up or down) and smoothly rotate around the world Y axis without affecting the other axis. Is there something I'm doing wrong here?
I'd really appreciate any help with this, as I've tried lots of different approaches and they all seem to have the same problem. It's really frustrating!
Here's my code, it's the very last line which is causing the problem:
function FixedUpdate () {
rigidbody.useGravity = false;
rigidbody.mass = 5500;
rigidbody.drag = 1.0;
rigidbody.angularDrag = 1.0;
var controllerInput = new Vector3(Input.GetAxis("Vertical"), 0, - Input.GetAxis("Horizontal"));
var inputPitch = controllerInput.x;
var inputRoll = controllerInput.z;
rigidbody.AddRelativeTorque(Vector3(inputPitch * 1,0,inputRoll * 1));
rigidbody.AddTorque(Vector3(0,inputRoll * -1,0));
Answer by KvanteTore · Mar 17, 2010 at 01:22 AM
The way I do it is to control the bank and pitch, and rotate the yaw such that the aircraft is always heading the same direction as the velocity.
As the lift is perpendicular to the velocity and the wing, the aircraft will get an acceleration in the direction of the bank, which will in turn lead to a change in the yaw.
Here is the core of my simplified flying physics component FYR
Vector3 v = this.rigidbody.velocity; float speed = v.magnitude;
Vector3 vLocal = transform.InverseTransformDirection(v);
float vForward = vLocal.y;
float vUp = -vLocal.z;
float vSide = vLocal.x;
//lift and drag forces are usually dependent on angle of attack
float angleOfAttack = - Mathf.Atan2(vUp, vForward);
float lc = LiftCoefficient.GetValue(angleOfAttack) * 1.5f;
float dc = DragCoefficient.GetValue(angleOfAttack);
lift = lc * v.sqrMagnitude;
drag = dc * v.sqrMagnitude;
//drag force is always antiparallel to the velocity
Vector3 dragForce = (-v).normalized * drag;
//lift force is perpendicular to velocity
Vector3 liftForce = (-transform.forward) * lift;
rigidbody.AddForce(dragForce + liftForce);
//adjust yaw to put the head towards where we are going
float yawSpring = -Mathf.Sign(vSide) * vSide * vSide * YawSpringCoefficient;
rigidbody.AddRelativeTorque(0, 0, yawSpring);
Answer by runevision · Mar 02, 2010 at 05:09 PM
I'm not sure exactly why the problem appears, but shouldn't you be straightening up the roll again when the Horizontal axis is no longer positive or negative? As it is it seems like the airplane will turn on it's side, and then just stay there until you turn the opposite way.
Thanks for the suggestion, I tried doing that, but if I get the script to correct the horizontal axis back to zero each frame, then the user can't ever pitch up or down. I think the answer might be in quaternions, but I don't understand how they work exactly.
Heres a waypoint package that handles pitch,yaw,bank and rotations for ground vehicles also. I am currently working on a physics option also.
Your answer
![](https://koobas.hobune.stream/wayback/20220612092401im_/https://answers.unity.com/themes/thub/images/avi.jpg)