• 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
0
Question by Philosophbot · Apr 30, 2020 at 10:53 AM · error3dflightforcesfloatingpoint

How can I create a working and semi-realistic lift force?

I was experimenting with making a flight simulator in Unity Engine and was coming across a problem where the plane would suddenly disappear due to floating-point and "direction normalized" errors. I think that this error has something to do with my lift equation in my PlaneController Script. My reasoning for this is that the errors occur when I try to do a loop in the plane, however it works fine if the plane is moving at slow speeds. One thing that I noticed about my simulator is that an unrealistically large amount of lift is generated. Since the lift equation takes into account the angle of the plane in radians, it seems to me that trying to do a loop just makes the lift force insanely large. I was wondering if anyone had any ideas as to how I could improve my thrust, drag, or lift equations. I posted my plane controller script below. As a heads up, the thrust, lift, and drag forces are added in the Fixed update section at the bottom of the script.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlaneController : MonoBehaviour {
     private Rigidbody plane;
 
     public float throttle;
     public float maxThrust;
     public float thrust;
 
     public float WingArea;
     public float WingSurfaceArea;
 
     private float yawRotation;
     private float airDensity;
     
     public float throttleAcceleration;
 
     // Use this for initialization
     void Start () {
         yawRotation = 0;
         plane = this.GetComponent<Rigidbody>();
         throttle = 0f;
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey(KeyCode.UpArrow))
         {
             if (throttle >= 100) throttle = 100;
             else throttle += 0.15f;
         }
         if (Input.GetKey(KeyCode.DownArrow))
         {
             if (throttle <= 0) throttle = 0;
             else throttle -= 0.15f;
         }
         if (Input.GetKey(KeyCode.A))
         {
             this.transform.Rotate(new Vector3(0, 0, 0.60f), Space.Self);
         }
         if (Input.GetKey(KeyCode.D))
         {
             this.transform.Rotate(new Vector3(0, 0, -0.60f), Space.Self);
         }
         if (Input.GetKey(KeyCode.W))
         {
             this.transform.Rotate(new Vector3(-0.60f, 0, 0), Space.Self);
         }
         if (Input.GetKey(KeyCode.S))
         {
             this.transform.Rotate(new Vector3(0.60f, 0, 0), Space.Self);
         }
         if (Input.GetKey(KeyCode.Q))
         {
             yawRotation -= 0.15f;
             if (yawRotation > -25f) this.transform.Rotate(new Vector3(0, -0.15f, 0), Space.Self);
             if (yawRotation <= -25f) yawRotation = -25f;
 
         }
         if (Input.GetKey(KeyCode.E))
         {
             yawRotation += 0.15f;
             if (yawRotation < 25f) this.transform.Rotate(new Vector3(0, 0.15f, 0), Space.Self);
             if (yawRotation >= 25) yawRotation = 25f;
         }
         else if (!Input.GetKey(KeyCode.Q) && !Input.GetKey(KeyCode.E))
         {
             if (yawRotation < 0)
             {
                 this.transform.Rotate(new Vector3(0, 0.15f, 0), Space.Self);
                 yawRotation += 0.15f;
             }
             if (yawRotation > 0)
             {
                 yawRotation -= 0.15f;
                 this.transform.Rotate(new Vector3(0, -0.15f, 0), Space.Self);
             }
         }
     }
 
     void FixedUpdate() 
     {
         plane.AddRelativeForce(Vector3.forward * thrust * 0.02f, ForceMode.Impulse);
         float xzVelocity = Mathf.Abs(this.GetComponent<Rigidbody>().velocity.z) + Mathf.Abs(this.GetComponent<Rigidbody>().velocity.x);
         plane.AddRelativeForce(Vector3.back * 0.02f * (1.229f * (xzVelocity + Mathf.Abs(this.GetComponent<Rigidbody>().velocity.y)) * (xzVelocity + Mathf.Abs(this.GetComponent<Rigidbody>().velocity.y))/2) * WingArea * 0.02f, ForceMode.Impulse);
         plane.AddForce((Vector3.up * 2 * 3.141f * Mathf.Deg2Rad * -plane.rotation.x * (xzVelocity * xzVelocity * 1.229f/2) * WingSurfaceArea)* 0.02f * 2, ForceMode.Impulse);
         if(thrust < maxThrust) thrust += throttleAcceleration * throttle / 100 * 0.02f;
     }
 }

Thanks for reading my post.

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 Philosophbot · May 03, 2020 at 02:45 AM 0
Share

As a side note, it seems like I was right in assu$$anonymous$$g that it has to do with the lift force. When I started decreasing it the plane starting disappearing less often. Another problem this lift force seemed to be causing is throwing off my camera, this also started to get fixed from a weaker lift force.

avatar image icehex Philosophbot · May 03, 2020 at 04:27 AM 0
Share

Couple of thoughts, should xzVelocity be the magnitude of the velocity in the xz plane? I.e. you'd take the square root of the sum of the squares of the z and x components. This would reduce xzVelocity, which would in turn reduce your lifting force.


While running this in the editor, you should be able to click on the plane object and watch how the values change in the inspector on the right as you fly around. If your forces are really strong maybe you run so far out in the Unity coordinates that your object doesn't render properly or at all?

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

197 People are following this question.

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

Related Questions

NullReferenceException: Object reference not set to an instance of an object, even what I am trying to get is not MonoBehaviour 1 Answer

BCE0004: Ambiguous reference 'Quest': Quest, Quest. 0 Answers

Float check not returning the right value. 1 Answer

Index out of range for no reason? 1 Answer

Debug.Log not working. 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