First person flight controls

Hey there, I've seem some similiar qustions but can't get the answer i want. I've tried for the alst 45 minutes to get a script up for a FPS controller for a flight machine. Since im sort of new to this, could someone show me how it works and explain a little why this and why that? So far I've added the mesh colliders around my map and tried experimenting with whatever script code could work for flying around and giving it sensivity so it gets the feel of a real plane. - How do i get a FPS controller to fly? - How do i add sensivity?

If you're aiming for the feel of an aeroplane, I would ditch the pre-packaged first person controller entirely. It's not suited to flight controls - it's specifically designed to act like a person walking around on the ground.

If you want your aeroplane (or flying creature, or flying superhero) to have a first-person view, simply parent your camera to the object, in the correct position for the view that you require (eg, inside the cockpit).

Instead, create a new script from scratch which implements a simplified version of the very basic essentials of flight. Make sure your aeroplane gameobject has a rigidbody, and for the script you will need to implement the following:

  • Use AddForce to apply a force in the plane's local forward direction. This is equivalent to the throttle - the power provided by the propellor or jet. Depending on how arcadey your game is, you could make this a constant force, or make it so the user can change the power during play.

  • A 'lift' force, which pushes the plane in its local up direction. The amount of lift is relative to its forward speed. Again, you'd use AddForce for this. To get the forward speed, you need to project the plane's rigidbody.velocity onto the plane's forward vector using Vector3.Project, and then get the magnitude of the result.

  • Banking controls. Banking is rotation around plane's local Z (forward), so tilts the plane left/right. You can approximate this in a simple manner by using AddTorque to affect the rotation of the plane's rigidbody.

  • Pitch controls. Pitch is rotation around the plane's local X, and so tips the nose up and down. Again, this can be approximated by applying torque to the plane's rigidbody just like banking.

  • Drag effect. You should increase the amount of drag and angularDrag on the plane in proportion to speed, to simulate the effect of air resistance.

For the banking and pitch controls, amount of torque should be proportional to the plane's forward speed (just like the lift force) - the faster you go, the more effect the plane's wing flaps have, and therefore the faster you turn. This also means when the plane isn't moving, it can't turn in any direction.

You might also want to implement rudder controls, which would basically be a repetition of the Banking/Pitch controls, but adding torque around the plane's local up axis. This type of rotation is sometimes called the Yaw or Slew of the plane.

Hope this is enough to get you started.

hi, just a thought,i little script on this post would help a lot of people, as this is the page that turns up on google the most about unity flight controls,

please post a script her,it would clarify a lot of things, like the proportions of the forces to be applied n stuff... (although i have managed to do this,im not sure if its properly implemented,it would be nise to compare it to a more accurate example script..)

Okay this is how I do it for my space-flight system (no aerodynamics/flight physics)

function FixedUpdate() {
   // Turn
   var h = Input.GetAxis("Mouse X");
   var v = Input.GetAxis("Mouse Y");

   rigidbody.AddRelativeTorque(0, h * 20, v * 20);
   // Other way you may like
   //transform.Rotate(0, h * 20, v * 20);
}

Also if `transform.forward` doesn't work try `transform.right`

Oh and if you want to be able to rotate:

    var rotateAmount = 5.0;
    function FixedUpdate() {
       if (Input.GetAxis("Horizontal") < 0) { transform.Rotate(rotateAmount, 0, 0); }
       else if (Input.GetAxis("Horizontal") > 0) transform.Rotate(-rotateAmount, 0, 0); }
    }

To rotate use left/right arrow keys or A/D.

This also works for Third Person controls

Wow. Good answer. A point to clarify. An airplane turns more by banking than it does by using the rudder. So, your controls might make the plane turn when it banks or when there's a combination of bank and rudder inputs.

Thanks a bunch guys, really helped me get started with this!

thanks for the great help.