Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Throttle and Inertia for rigidbody (airplane)

Discussion in 'Physics' started by Jivu286, Nov 22, 2023.

  1. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    Hello, I'm a total beginner still wrapping my head around the basics of c#. I'm trying to get a simplified arcadey approximation of war thunder flight. So far thanks to this mouse flight code from github and sifting through a bunch of tutorials I have everything I need except for satisfactory physics. The plane responds to throttle more like a car, it accelerates and decelerates way too fast, it doesn't really glide, and it doesn't pitch down and gain speed in a dive when there's no throttle (I do have gravity enabled).

    I have a kind of ratchet throttle system rigged up, I don't know if adjusting or replacing it will be necessary. This is the relevant part of the code so far:

    Code (CSharp):
    1. [Header("Physics")]
    2. [Tooltip("Force to push plane forwards with")] public float thrust = 0f;
    3. [Tooltip("Maximum thrust at full throttle.")] public float maxThrust = 200f;
    4. [Tooltip("rate of thrust increase")] public float thrustIncrement = .2f;
    5. public float throttle = 0f;
    6. public float throttleIncrement = .2f;
    7.  
    8. private void HandleInputs()
    9.         {
    10.             if (Input.GetKey(KeyCode.LeftShift)) throttle += throttleIncrement;
    11.             else if (Input.GetKey(KeyCode.LeftControl)) throttle -= throttleIncrement;
    12.             throttle = Mathf.Clamp(throttle, .1f, 100f);
    13.     }
    14.  
    15. private void FixedUpdate()
    16. {
    17.    rigid.AddRelativeForce(Vector3.forward * maxThrust * throttle, ForceMode.Force);
    18.  
    19. rigid.AddForce(Vector3.up * speed * lift);
    Can anyone point me to relevant code for accelerating and decelerating dynamically, as well as possibly how to make the nose pitch downwards and gain speed when falling?
     
    Last edited: Nov 22, 2023
  2. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    75
    I think you'll have a problem here because your lift force is always in the world-up direction (opposing gravity).
    So the faster you fall, the more world-up-lift you'll generate.
    I think you'd want something like:
    Code (CSharp):
    1. var forwardVelocity = Vector3.Dot( rigid.velocity, rigid.transform.forward ); //Should maybe use rigid.rotation * Vector3.forward?
    2. if ( forwardVelocity < 0 )
    3.     forwardVelocity = 0; //We're going backwards, no lift pls
    4. rigid.AddRelativeForce(Vector3.up * forwardVelocity * lift); //Use relative force: our lift should be locally up, not world up.
    ^^^I've not time to test this, sorry.

    I guess you'd want your centerOfMass a little forwards to make it pitch forwards when there's no other forces being applied.
    I'm not sure if you'd want to apply your forces at some offset from the center of mass (slight googling around suggests you have upward wing lift and downward tail "lift").
    I guess more mass will make the plane respond more slowly to your forces (or, smaller forces, I guess:).
    I guess you'd want some kind of rigid.drag going on somewhere, or some force of your own to simulate it.

    If HandleInputs is being called from Update, I think you should += throttleIncrement * Time.deltaTime to make it independent of the frame-rate (you'll probably need to increase throttleIncrement).

    There used to be a neat aircraft controller example in the Unity Standard Assets

    It looks like Unity deprecated it (not sure there's any replacement), but I bet it's available online somewhere
    (discussion of it here https://forum.unity.com/threads/standard-assets-are-not-available-in-2021-release.1137559/)
     
    Jivu286 likes this.
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,171
  4. Jivu286

    Jivu286

    Joined:
    Nov 19, 2023
    Posts:
    18
    Thank you for the reply. Adding deltatime to my throttle ramp speed did solve a minor issue where how long it took to open the throttle fully was inconsistent, I was trying to adjust that by tweaking throttleIncrement but your way is better.

    I am still having trouble getting the body to accelerate and decelerate in a reasonable way, or figuring out how to code the relationship between throttle and thrust. I'm not finding any good tutorials or forum posts because so many are using transforms instead of physics :/
     
  5. codebiscuits

    codebiscuits

    Joined:
    Jun 16, 2020
    Posts:
    75
    I'd definitely check out the example in the Standard Assets if you haven't already, from memory, that was physics-based.