Unity 3D Code Snippet – Flight Script

Roll, yaw and pitch axis definition for an air...

Flying is a simple concept in games. An object that can move in 3 dimensions? I am all over that! But how about rotating to the forward vector? How about a constant velocity? Here is a quick way to get your game object up in the air (that joke hurt to type).

Set Up Your Player

There really isn’t much you have to do to set up you player object. Here’s a few simple steps:

  1. Create a GameObject (either Empty or a Primitive (cube, sphere, etc). If you pick empty, make sure you give it a model).
  2. Name it (something descriptive, like “PlayerPlane” or something).
  3. Give it a Rigidbody (Component->Physics->Rigidbody).
  4. Create an empty C# script.
  5. Attach the script to your new GameObject.
That’s pretty much it, except for one more thing: The Rigidbody. Your object has physics, now, and he’s going to have to be affected by other objects correctly. If you don’t set this up right, your flying object may get tapped by anything and start spiraling out of control.
Here are the settings I use. Feel free to mess around with your own.
I froze the rotation because every time I hit another object I lost control of my ship, having to lean on the arrow keys to keep it steady (it was actually a pretty cool effect, like phugoid or dutch rolling in aeronautics).
Now, the function for the script is pretty straight forward, but I’ll break it down bit-by-bit (full code available at the bottom).
    public float AmbientSpeed = 100.0f;

    public float RotationSpeed = 200.0f;

We give the script these two members. AmbientSpeed is minimum speed of the aircraft (in this example, the aircraft is always moving along its forward axis. You can remove this or change it so the object can move backwards and such). RotationSpeed is the speed at which the object turns. Simple enough to understand, right?

Now we’re getting into our UpdateFunction(). Why UpdateFunction() instead of using Update() or FixedUpdate()? Well, I’ve answered my own question: It could be used for either case. But, since I’m using a Rigidbody, it would make sense to use this for FixedUpdate(). I just like to keep code modular.

void UpdateFunction()
    {

        Quaternion AddRot = Quaternion.identity;
        float roll = 0;
        float pitch = 0;
        float yaw = 0;

This is initialization code. Obvious. AddRot is our temporary variable for storing the rotation value that we’ll be rotating by. roll, pitch and yaw are for storing our input values.

        roll = Input.GetAxis("Roll") * (Time.deltaTime * RotationSpeed);
        pitch = Input.GetAxis("Pitch") * (Time.deltaTime * RotationSpeed);
        yaw = Input.GetAxis("Yaw") * (Time.deltaTime * RotationSpeed);

Here, we’re getting our input (defined by “Roll,” “Pitch,” and “Yaw” which are defined in the Input Manager in Edit->Project Settings->Input), and multiplying it by (Δtime multiplied by our rotation speed). This gives us the angles at which we want to rotate.

        AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
        rigidbody.rotation *= AddRot;

We take those angle values, chuck them into a Vector3, and then directly modify our AddRot.eulerAngles member to rotate it to those values. We then multiply our rigidbody.rotation by AddRot and Viola! Rotation.

        Vector3 AddPos = Vector3.forward;
        AddPos = Ship.rigidbody.rotation * AddPos;
        rigidbody.velocity = AddPos * (Time.deltaTime * AmbientSpeed);
    }

Last, but not least, we create AddPos. AddPos will be used to define our velocity. We set it to the product of our rigidbody.rotation by Vector3.forward, and set the rigidbody.velocity to AddPos multiplied by (Δtime multiplied by our ambient speed). If you wanted to have a boost speed, it would be (Δtime multiplyed by (ambient speed plus boost speed)).

And there you go. A player controlled, flying… whatever. Keep in mind this is pretty basic, and you can probably go further to optimize and make it more robust. Any tips or modifications would be great in the comments.

Once again, here’s all the code together. Thanks for checking out the tutorial. Take care!

    public float AmbientSpeed = 100.0f;

    public float RotationSpeed = 200.0f;

void UpdateFunction()
    {        Quaternion AddRot = Quaternion.identity;
        float roll = 0;
        float pitch = 0;
        float yaw = 0;
        roll = Input.GetAxis("Roll") * (Time.deltaTime * RotationSpeed);
        pitch = Input.GetAxis("Pitch") * (Time.deltaTime * RotationSpeed);
        yaw = Input.GetAxis("Yaw") * (Time.deltaTime * RotationSpeed);
        AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);
        rigidbody.rotation *= AddRot;
        Vector3 AddPos = Vector3.forward;
        AddPos = Ship.rigidbody.rotation * AddPos;
        rigidbody.velocity = AddPos * (Time.deltaTime * AmbientSpeed);
    }

You guys asked for it, here is a sample project set up using the flight script. Enjoy.

Advertisements