Rigidbody Flight Simulator: help with script?

Hello all,

I’m currently working on a flight simulator project just for fun. I’m trying to work on the pitch, yaw, roll, acceleration, and braking, however it’s not working like I planned. I’m trying to make it to where I can pitch up to takeoff from the runway, however it doesn’t let me do that until I’ve gone clear off the runway (currently an elongated cube). I want to be able to pitch up when the plane reaches a certain speed. When I do pitch up after I’ve fallen off the runway, the plane doesn’t go in the direction it faces. It just keeps going forward parallel to the runway. When it loses some speed, it falls down. I want the rigidbody feel of a flight simulator, but I don’t want the plane to fall that drastically after a little bit of flying. Could someone look at my code and tell me what I’m doing wrong?

P.S. It’s just the script, you can throw it on a cube or something in your own project to see what I mean.

using UnityEngine;
using System.Collections;

public class RBPlaneCntrl : MonoBehaviour {
	
	public float thrust;					// Magnitude of Velocity
	public Rigidbody rb; 					// Declares Rigidbody
	public float MaximumVelocity = 500;		// Velocity the plane can reach at max speed.
	public float MinimumVelocity = 1;		// Velocity the plane can reach at minimum speed.
	public float AccIncrement = 2; 			// Increment by which the plane accelerates & decelerates.
	public float pitchTorque = 100;			// The force that plane will turn while pitching.
	public float rollTorque = 100;			// The force that plane will turn while rolling.
	public float yawTorque = 100;			// The force that plane will turn while yawing.
											// For additional inf on YPR, look at this: 
												// http://www.toymaker.info/Games/assets/images/yawpitchroll.jpg

	void Start () { 						// Initialization
		rb = GetComponent<Rigidbody>();		// Sets the rigidbody to the RB component attached to the GameObject.
		thrust = 1; 						// Sets the thrust to 1, otherwise other methods wouldn't work.
	}
	
	void Update () {		// Updates everything once per frame.

		if(Input.GetKey("q") && (thrust <= MaximumVelocity && thrust > MinimumVelocity-1 )) { 	// Accelerate plane based on conditions given.
			thrust += AccIncrement;																// Changes thrust by an increment of AccIncrement.
			AcceleratePlane();																	// Calls the AcceleratePlane function.
		}

		if (Input.GetKey("e") && (thrust > MinimumVelocity)) {	// Decelerate plane based on coordinates given.
			thrust -= AccIncrement * 10;						// Changes thrust by an increment of AccIncrement times ten.
			DeceleratePlane();									// Calls the DeceleratePlane function.
		}

		PitchPlane();
	}
	
	void AcceleratePlane() {										// Accelerates the plane forward.
		rb.AddRelativeForce(transform.forward * thrust);			// Add force to the RB foward * thrust
	}
	
	void DeceleratePlane() {										// Brakes the plane.
		rb.AddRelativeForce(-transform.forward * thrust);			// Add neg force to the RB forward * thrust.
	}

	void PitchPlane() {
		float turn = Input.GetAxis("Vertical");
		rb.AddTorque(transform.right * pitchTorque * turn * 10);
	}
}

Well, as for simulation, airplanes doesn’t necessarily apply lift at the center of the body. They also usually have wheels at the front and somewhere after the center of mass - but not at the very end. If they did, they wouldn’t be able to get off the runway, just like you describe. Because imagine what would happen - the tail of the craft would push down while the nose of the craft would push up. If the tail has wheels, and the wheels are in contact with the runway, it’ll push on the runway, which won’t budge. So the plane can’t lift because it can’t pull the nose up (or rather, push the tail down). They need to pivot around the back wheels, so the tail of the aircraft can be lowered and the nose raised - therefore the wheels of an aircraft are near the center of mass.

Notice the positioning of wheels in aircraft and it might make sense - far ahead.

(Making a nice simulation of an aircraft is a little more involved than applying torque around the center of the rigidbody - if you aren’t familiar with airplane physics, you could toy around with Kerbal Space Program which let you build planes yourself to get a sense of the force vectors - there are plenty of tutorials - I really recommend it if you don’t know about aircraft dynamics)

I guess, to fake it so you can pitch up, apply torque at the tail of the aircraft so the nose can raise without the tail slamming the ground - or apply a little up force when you pitch up and you’re close to ground. It won’t be an accurate simulation, but at least you should be able to get the craft off the runway.

When I do pitch up after I’ve fallen off the runway, the plane doesn’t go in the direction it faces

It’s because you pass transform.forward instead of Vector3.forward, into AddRelativeForce, I guess.

Also note that without wing air drag simulation, your plane won’t climb or dive, when you cut the engine and pitch up or down. It’ll behave more like a craft in vaccuum, or like a rocket without fins that magically rotate around its center.

Notice how one can steer and land (well, some definition of landing at least) a plane without engine power.