Helicopter Controller for Unity

Creating a helicopter game in Unity can be a fun project for game developers. In this tutorial, I'll guide you through the process of creating a simple helicopter game using Unity and C#. We'll cover how to set up the helicopter's movement, controls, and basic physics.

Step 1: Setting Up the Project

  • Open Unity and create a new 3D project.
  • Set up your project settings as needed (e.g., naming, location).
  • Import any assets you'll be using, such as helicopter models, terrain, and skyboxes.

Step 2: Creating the Helicopter GameObject

  • Create a new empty GameObject ('GameObject -> Create Empty').
  • Rename the GameObject to "Helicopter" for clarity.
  • Attach a 3D model of a helicopter to the GameObject by dragging it into the scene.

Step 3: Adding Rigidbody Component

  • Select the helicopter GameObject.
  • Click "Add Component" in the Inspector window.
  • Search for "Rigidbody" and add the Rigidbody component to the helicopter.
  • Adjust the Rigidbody settings to match the weight and physics properties of your helicopter model.

Step 4: Writing Helicopter Movement Script

  • Now, we'll create a C# script to handle the helicopter's movement.

'HelicopterController.cs'

using UnityEngine;

public class HelicopterController : MonoBehaviour
{
    public float maxSpeed = 10f; // Maximum speed of the helicopter
    public float maxRotationSpeed = 5f; // Maximum rotation speed of the helicopter
    public float acceleration = 2f; // Acceleration factor for speed
    public float rotationAcceleration = 1f; // Acceleration factor for rotation speed
    public Transform mainRotor; // Drag the main rotor GameObject here in the Inspector
    public Transform tailRotor; // Drag the tail rotor GameObject here in the Inspector

    private Rigidbody rb;
    private float currentSpeed = 0f;
    private float currentRotationSpeed = 0f;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        // Get user input for movement
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");

        // Calculate movement direction
        Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);

        // Apply movement to the helicopter
        rb.AddRelativeForce(movement * acceleration);

        // Calculate new speed based on acceleration
        currentSpeed = Mathf.Clamp(currentSpeed + acceleration * Time.deltaTime, 0f, maxSpeed);

        // Get user input for rotation
        float rotationInput = Input.GetAxis("Rotation");

        // Calculate rotation
        Quaternion rotation = Quaternion.Euler(0f, rotationInput * maxRotationSpeed, 0f);

        // Apply rotation to the helicopter
        rb.MoveRotation(rb.rotation * rotation);

        // Rotate main rotor
        mainRotor.Rotate(Vector3.up * currentSpeed * Time.deltaTime * 100f);

        // Rotate tail rotor
        tailRotor.Rotate(Vector3.right * currentSpeed * Time.deltaTime * 500f);

        // Calculate new rotation speed based on acceleration
        currentRotationSpeed = Mathf.Clamp(currentRotationSpeed + rotationAcceleration * Time.deltaTime, 0f, maxRotationSpeed);
    }
}

Step 5: Attaching the Script

  • Create a new C# script in your Unity project.
  • Copy and paste the code provided above into the script.
  • Attach the script to the Helicopter GameObject in the Inspector window.

Step 6: Configuring Input

  • Go to 'Edit -> Project Settings -> Input Manager'.
  • Set up input axes for Horizontal, Vertical, and Rotation. You can use keys or joystick axes for input.

Step 7: Testing

  • Press Play in the Unity Editor to test your helicopter game.
  • Use the configured input keys to control the helicopter's movement and rotation.
  • Adjust the 'maxSpeed', 'maxRotationSpeed', 'acceleration', and 'rotationAcceleration' variables in the script to fine-tune the helicopter's behavior.

Conclusion

You've created a basic helicopter game in Unity. From here, you can expand the game by adding obstacles, terrain, enemies, and more advanced features.