- Home /
Aircraft controller script
Hi,
I have the following script to control my airplane in air. I 'stole' some values from the wikipedia page of the F-16 aircraft and I applied those to the model in game.
void FixedUpdate()
{
float L = 0.5f * Globals.AirDensity(transform.position.y, AltUnit.Meters) * rigidbody.velocity.magnitude * m_Planform * 2.0f * Mathf.PI * Vector3.Angle(transform.forward, Vector3.up);
float pitch = Input.GetAxis("pitch");
float roll = Input.GetAxis("roll");
float yaw = Input.GetAxis("yaw");
float finalRotFactor = m_RotSpeed * (Thrust / MaxThrust);
rigidbody.AddRelativeTorque(Vector3.right * pitch * finalRotFactor, ForceMode.Impulse);
rigidbody.AddRelativeTorque(Vector3.up * yaw * finalRotFactor, ForceMode.Impulse);
rigidbody.AddRelativeTorque(Vector3.forward * roll * finalRotFactor * 4, ForceMode.Impulse);
rigidbody.AddForce(transform.forward * Thrust * 2, ForceMode.Impulse);
rigidbody.AddForce(Vector3.up * L, ForceMode.Force);
m_Lift = L;
m_Velocity = rigidbody.velocity.magnitude;
}
Now it kinda works. I can fly around and all that, but the moment I turn or bank, it feels like I'm drifting through the air. It's like it still applies the force towards the old angle that the aircraft was in.
How do I stablize it to go forward and not slip 'n' slide all over the place? And... is this script correct? Am I doing something wrong? Thanks!
can you give us a java file to downlowed cuse java script isnt opening for some resson
Answer by Meltdown · Feb 07, 2012 at 05:46 PM
You need to apply 'air resistance' to the aircraft based on how it presents itself to the oncoming air. So if it rolls to the left and pitches up relative air pressure needs to be applied to the underside of the air craft.
Answer by Wiegje · Feb 07, 2012 at 05:59 PM
So I need to add another force in the direction of the rotation? Not to the counter direction?
Don't post comments as answers, reply to answers like I've done here. Wiegje, aircraft physics are rather complicated to explain. I'd suggest first reading up on the X-Plane.org website on how planes work, that is a nice introduction. Then get more into the physics by reading up on the basics by googling it.
Whoops, sorry. Well, apart from the slippery feel, the airplane feels right and moves right.
Try playing with the drag and angular drag settings as well.
Can someone please point me to the original F-16 script in this thread? I tried converting the code here to Javascript and using it, but it says there are unknown identifiers for AltiUnit and Globals. $$anonymous$$y question is at http://answers.unity3d.com/questions/231731/flight-model-for-rigidbody-physics.html?viewedQuestions=214843&viewedQuestions=230459&viewedQuestions=56334&viewedQuestions=29198 and it's a really confusing issue with trying to use a rigidbody for a flight model when the rigidbody uses gravity.