Throttle and Inertia for rigidbody (airplane)

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:

[Header("Physics")]
[Tooltip("Force to push plane forwards with")] public float thrust = 0f;
[Tooltip("Maximum thrust at full throttle.")] public float maxThrust = 200f;
[Tooltip("rate of thrust increase")] public float thrustIncrement = .2f;
public float throttle = 0f;
public float throttleIncrement = .2f; 

private void HandleInputs()
        {
            if (Input.GetKey(KeyCode.LeftShift)) throttle += throttleIncrement;
            else if (Input.GetKey(KeyCode.LeftControl)) throttle -= throttleIncrement;
            throttle = Mathf.Clamp(throttle, .1f, 100f);
    }

private void FixedUpdate()
{
   rigid.AddRelativeForce(Vector3.forward * maxThrust * throttle, ForceMode.Force);

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?

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:

var forwardVelocity = Vector3.Dot( rigid.velocity, rigid.transform.forward ); //Should maybe use rigid.rotation * Vector3.forward?
if ( forwardVelocity < 0 )
    forwardVelocity = 0; //We're going backwards, no lift pls
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 Standard Assets are not available in 2021 release )

1 Like

Here’s how to post code on the forums: Using code tags properly

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 :confused:

I’d definitely check out the example in the Standard Assets if you haven’t already, from memory, that was physics-based.