• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Wiegje · Feb 07, 2012 at 03:51 PM · rigidbodycontrollerairplaneaircraft

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!

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image laith · Mar 07, 2015 at 08:17 PM 0
Share

can you give us a java file to downlowed cuse java script isnt opening for some resson

2 Replies

· Add your reply
  • Sort: 
avatar image

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.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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?

Comment

People who like this

0 Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Meltdown · Feb 07, 2012 at 06:01 PM 0
Share

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.

avatar image Wiegje · Feb 07, 2012 at 06:05 PM 0
Share

Whoops, sorry. Well, apart from the slippery feel, the airplane feels right and moves right.

avatar image Meltdown · Feb 07, 2012 at 06:10 PM 0
Share

Try playing with the drag and angular drag settings as well.

avatar image MachCUBED · Mar 25, 2012 at 04:52 AM 0
Share

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. My 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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Airplane controller for game object? 0 Answers

Airplane flying using accelerometer 0 Answers

Is there a script for moving a rigidbody without using a character controller? 1 Answer

funky rigidbody movement 0 Answers

How can I make a Character Controller collide with a Rigidbody or another Character Controller 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges