Plane Flight C# Script for Unity

A simple script to achieve a rigid body flying through the air to simulate a plane.

Make sure to create and update your input settings with 3 new inputs:

  • Roll
  • Pitch
  • Yaw

Create a new class in Unity called FlightScript and copy this code into it:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(Rigidbody))]
public class FlightScript : MonoBehaviour {
	
	public float AmbientSpeed = 100.0f;

    public float RotationSpeed = 100.0f;

    private Rigidbody _rigidBody;

	// Use this for initialization
	void Start () 
	{
        _rigidBody = GetComponent<Rigidbody>();

    }
	
	// Update is called once per frame
	void Update () 
	{
	
	}
	
	void FixedUpdate()
	{
       //Since the plane is a rigidbody we need to update the plane in a FixedUpdate method
		UpdateFunction();
	}
	
	void UpdateFunction()
    {
//create a new quaternion to apply the rotation to.
        Quaternion AddRot = Quaternion.identity;

//intialise our floats to handle roll, pitch and yaw.
        float roll = 0;
        float pitch = 0;
        float yaw = 0;

//get the axis inputs from the input manager
        roll = Input.GetAxis("Roll") * (Time.fixedDeltaTime * RotationSpeed);
        pitch = Input.GetAxis("Pitch") * (Time.fixedDeltaTime * RotationSpeed);
        yaw = Input.GetAxis("Yaw") * (Time.fixedDeltaTime * RotationSpeed);

//add the inputs to the quaternion we created above
        AddRot.eulerAngles = new Vector3(-pitch, yaw, -roll);

//apply the rotation to our rigidbody
        _rigidBody.rotation *= AddRot;
        Vector3 AddPos = Vector3.forward;
        AddPos = _rigidBody.rotation * AddPos;

//create a forward velocity for the plane.
        _rigidBody.velocity = AddPos * (Time.fixedDeltaTime * AmbientSpeed);
    }
}

Also if you want a smooth camera follow for the plane you can utilize this script:

Again create a new script and call it SmoothFollow and copy this code into it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SmoothFollow : MonoBehaviour
{
    [SerializeField]
    Transform target;

    public Vector3 Offset = new Vector3(0, 2f, 10f);

    public float heightDamping = 2.0f;
    public float rotationDamping = 3.0f;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // LateUpdate is called once per frame AFTER all the Updates
    // Source: https://gamedev.stackexchange.com/questions/130621/turning-the-camera-when-the-player-turns
    void LateUpdate()
    {
        if (target == null) return;

        float wantedRotationAngle = target.eulerAngles.y;
        float wantedHeight = target.position.y + Offset.y;

        float currentRotationAngle = transform.eulerAngles.y;
        float currentHeight = transform.position.y;

        // Damp the rotation around the y-axis
        currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

        // Damp the height
        currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

        // Convert the angle into a rotation
        var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);

        // Set the position of the camera on the x-z plane to:
        // distance meters behind the target
        transform.position = target.position;
        transform.position -= currentRotation * Vector3.forward * Offset.z;

        // Set the height of the camera
        transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);

        // Always look at the target
        transform.LookAt(target);
    }
}

After you have updated the script, you’ll want to apply the script to the camera and set the target transform field as the plane.

And you’re done! You’ll have a flying rigidbody plane now :star_struck: