Help with Helicopter Controller

Hey

I’m following this tutorial: Tutorial | Unity3D & C# | Helicopter Controller – Dev Blog | Ben Rosen

but I can’t get the Helicopter.cs script to work, in the Update function it gives me an error on “_controls.HandleInput();”.

It gives me an error like this: " Type HelicopterControls' does not contain a definition for HandleInput’ and no extension method HandleInput' of type HelicopterControls’ could be found"

HelicopterControls.cs:

using System.Collections;
using UnityEngine;

public class HelicopterControls {

    public float Pitch
    {
        get;
        protected set;
    }

    public float Roll
    {
        get;
        protected set;
    }

    public float Yaw
    {
        get;
        protected set;
    }

    public float Collective
    {
        get;
        protected set;
    }

    public float Throttle
    {
        get;
        protected set;
    }
}

PilotHelicopterControls.cs:

using System.Collections;
using UnityEngine;

public class PilotHelicopterControls : HelicopterControls {

    public void HandleInput()
    {
        Pitch = Input.GetAxis("Vertical");
        Roll = Input.GetAxis("Horizontal");
        Yaw = Mathf.InverseLerp(-1f, 1f, Input.GetAxis("L Stick H"));
        Collective = Mathf.InverseLerp(-1f, 1f, Input.GetAxis("L Stick V"));
        Throttle = 1f;
    }

}

Helicopter.cs:

using System.Collections;
using UnityEngine;

public class Helicopter : MonoBehaviour {

    [Header("Components")]
    public Rigidbody _mainRotor;
    public Rigidbody _body;
    public Transform _tailRotor;

    private LinearForceApplicator _lift;
    private RotationalForceApplicator _mainRotorTorque;
    private RotationalForceApplicator _bodyTorque;
    private RotationalForceApplicator _bodyCounterTorque;
    private RotationalForceApplicator _pitchTorque;
    private RotationalForceApplicator _rollTorque;
    private HelicopterControls _controls;
    private Rotater _tailRotorRotater;

    void Start()
    {
        _lift = new LinearForceApplicator(_body, 1000062, Vector3.up);
        _mainRotorTorque = new RotationalForceApplicator(_mainRotor, 3, 240, Vector3.down);
        _bodyTorque = new RotationalForceApplicator(_body, 25000, 120, Vector3.down);
        _bodyCounterTorque = new RotationalForceApplicator(_body, 50000, 120, Vector3.up);
        _pitchTorque = new RotationalForceApplicator(_body, 20000, 100, Vector3.right);
        _rollTorque = new RotationalForceApplicator(_body, 15000, 100, Vector3.back);
        _controls = new PilotHelicopterControls();
        _tailRotorRotater = new Rotater(_tailRotor, Vector3.right, 500);

        // This should account for any weird wobbling caused by misaligned pivots.
        _body.centerOfMass = Vector3.zero;
    }

    void Update()
    {
        _controls.HandleInput();
    }

    void FixedUpdate()
    {
        // Throttle
        _mainRotorTorque.ApplyForcePercentage(_controls.Throttle);
        _bodyTorque.ApplyForcePercentage(_mainRotorTorque.PercentMaxRPM);

        // Yaw
        _tailRotorRotater.Rotate(_mainRotorTorque.PercentMaxRPM);
        _bodyCounterTorque.ApplyForcePercentage(_mainRotorTorque.PercentMaxRPM * _controls.Yaw);

        // Collective
        _lift.ApplyForcePercentage(_mainRotorTorque.PercentMaxRPM * _controls.Collective);

        // Cyclic
        _pitchTorque.ApplyForcePercentage(_mainRotorTorque.PercentMaxRPM * _controls.Pitch);
        _rollTorque.ApplyForcePercentage(_mainRotorTorque.PercentMaxRPM * _controls.Roll);
    }

}

What’s wrong?

Well, the error message is right. You’ve got a _controls property whose type is HelicopterControls. And you’re trying to call HandleInput() on that. But HelicopterControls doesn’t have any such method.

Now there happens to be a HelicopterControls subclass called PilotHelicopterControls, which does have such a method. But that doesn’t matter, because _controls is of type HelicopterControls, which does not.

It looks to me like this is just an error in Ben’s tutorial. There are several possible fixes, but the easiest would be to just change the type of your _controls field. In line 17 of Helicopter.cs, change the type from HelicopterControls to PilotHelicopterControls. Now it’s of a type that really does have a HandleInput method, so the call will work.

And note that you should probably contact Ben Rosen (the author of the tutorial) about this. I notice he has a comment form on the bottom of the page. I’m sure he’ll appreciate the feedback, and may point out some other solution he had in mind.

1 Like