Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Oct 04, 2013 at 01:13 PM by Ranger-Ori for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ranger-Ori · May 01, 2012 at 05:12 PM · physicsflightmechanics

Flight mechanics 2D, variables vs forces

Hi!

I am working on 2D flight sim, the only rotation that is important is the pitching up and down for the airplane. No wings are needed for this. I'm using 4 variables: Lift, Drag, Gravity, Force (which is the thrust).

Let me show you 2 of my scripts.

number 1, forces based.

 var liftValue             = 0.0;
 var liftCalc                = 0.0;
 
 var dragValue         = 0.0;
 var dragCalc             = 0.0; //Drag calculations
 
 var maxSpeed            = 0.0;
 
 var throttle            = 0.0;
 var gravityValue        = 0.0;
 var rotateSpeed      = 0.0;
 var objAngle            = 0.0;
 var xForces             = 0.0; //sum of all x forces
 var yForces             = 0.0; //sum of all y forces
 
 
 function Update(){
 
 //For increasing/decreasing speed 
 if(Input.GetButtonDown("ThrottleUp"))
 throttle++;
 
 if(Input.GetButtonDown("ThrottleDown"))
 throttle--;
 
 //For pitching up and down the airplane
 var rotationZ : float = Input.GetAxis ("Pitch") * rotateSpeed;
 transform.RotateAround(Vector3.forward,rotationZ * Time.deltaTime);
 
 }
 
 
 function FixedUpdate(){
 
 
 objAngle = transform.localEulerAngles.z;
 //This is getting the attack angle.
 var vel  = rigidbody.velocity.magnitude;
 //This is receiving the airplane speed.
 dragCalc = vel*dragValue/10;
 //This is calculating the drag based on the speed.
 liftCalc    = vel*liftValue/10;
 //This is calculating the lift based on the speed.
 
 //I've given these calculations my own kind of values.
 
 //You can ignore this, this is simply for the airplane 
 //to move straight without any effect at start
 if (objAngle == 0)  
     rigidbody.AddRelativeForce(throttle,0,0);
 
 //Pitching up.
 if (objAngle>0&&objAngle<=90) 
     {
     xForces  = Mathf.Sin(objAngle*Mathf.PI/180)*(throttle*5-liftCalc-dragCalc);
     yForces  = Mathf.Cos(objAngle*Mathf.PI/180)*(liftCalc+throttle-dragCalc)-gravityValue;
     }
     
 //Pitching down.
 if (objAngle>=270&&objAngle<360)
     {
     xForces  = Mathf.Cos(objAngle*Mathf.PI/180)*(throttle*5+liftCalc+dragCalc);
     yForces  = Mathf.Sin(objAngle*Mathf.PI/180)*(liftCalc+dragCalc-throttle)-gravityValue;
     }
 if(vel<maxSpeed)
     constantForce.relativeForce = Vector3(xForces,yForces,0);
     //I'll note that I've tried to put in different use of rigidbody
     //I tried to rigidbody.AddRelativeForce or AddForce.
 else
     vel--;
     print(vel);
     //To check up and critisize my velocity.
     
 }

My airplane never stops, the forces are always adding, how can I limit them? Or is there another way of putting them?

and my second script, variables format, basically was calculated by putting the velocity in the translate command: F = ma a = v/t v = F*t/m

(Basically the same script except this)

 function FixedUpdate(){
 
 
 objAngle = transform.localEulerAngles.z; //This is getting the attack angle.
 
 if (objAngle == 0)
     transform.Translate(0.5, 0 , 0);
 
 if (objAngle>0&&objAngle<=90)
     {
     xForces  = Mathf.Cos(objAngle*Mathf.PI/180)*(velSpeed-liftValue-dragValue);
     yForces  = Mathf.Sin(objAngle*Mathf.PI/180)*(liftValue+velSpeed-dragValue)-gravityValue;
     }
 if (objAngle>=270&&objAngle<360)
     {
     xForces  = Mathf.Cos(objAngle*Mathf.PI/180)*(velSpeed+liftValue+dragValue);
     yForces  = Mathf.Sin(objAngle*Mathf.PI/180)*(liftValue+dragValue-velSpeed)-gravityValue;
     }
     
 transform.Translate(xForces*Time.deltaTime,yForces*Time.deltaTime,0 , Space.World);
 }

Comment
Add comment · Show 2
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 chainedlupine · May 02, 2012 at 02:50 AM 0
Share

It looks like you're mixing both rigidbody physics via AddForceRelative and manipulating the rotation of the transform directly. This isn't a good idea as it can cause collision response problems. You should decide on one method, either control the rigidbody kinematically or with physics. If it is being done kinematically, then it's fairly easy to limit how fast your plane can move or how far it can rotate.

avatar image Ranger-Ori · May 02, 2012 at 09:01 AM 0
Share

And if it done physically? can I limit the forces? cause they never stop. And by rotating with physics, do you mean AddTorque?

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by alexc1995 · May 07, 2012 at 10:10 AM

One of the problems with adding force is that you continuously add the force. This means that you're adding the force leftover from the last movement to this force, meaning there's even more force on the object. This is your "moving faster and faster" problem.

In my opinion, the best way to have a consistent speed is to use translates/transformations. Though there are probably other ways of doing it, this is the easiest way to do it.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Flight script problem 0 Answers

Hop (Ketchapp) Mechanic (Jump between platforms with certain velocity while controlling object on X axis) 1 Answer

Error in script 1 Answer

Problem: Accessing Other Script Variables 3 Answers


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