Drone Controls with the New Input System in Unity 2021

GameDev Dustin
4 min readJun 8, 2022

--

In this article we’ll look over the Action Map and input code that controls this flying drone.

First of all, let’s look at the Action Map for the Drone controls.

When the input system switches to this Action Map, we use WSAD keys for tilt control, Space & V keys for vertical lift, and the Left & Right arrow keys for rotation.

Tilt is stored similar to our normal character movement as we are once again using WSAD keys.
This is stored as an Action Type of Value with a Control Type of Vector2.

Essentially, you get the following Vector 2 value when a particular key is pushed:

W: 0,1 | S: 0, -1 | A: -1, 0 | D: 1, 0

Looking at the Drone script which is a component on the Drone game object, we first need a “Using UnityEngine.InputSystem” statement.

While all the class variables shown play a role, the two highlighted specifically interact with our Input System.

In either the Awake() or Start() method, we want to assign our _inputActions variable as a new InputActions class which is a generated C# class & script from the Action Map asset.

Here we have the methods that will control enabling and disabling the Drone Action Map input controls.

First we must enable the Action Map, then we create a listener/subscriber method for when the ExitFlightMode (escape button) is performed.

When the player exits drone mode, we disable the Drone Action Map.
While not shown here, this returns control to the Character Action Map which happens in another script.

Any time game object movement is involved, we know we can look for it in the Update() or FixedUpdate() methods.

This particular script actually made use of both, though I’m not sure I’d recommend that.

Our Update() method checks for if flight mode is active and then calls CalculateTilt() and CalculateMovementUpdate() methods.

In CalculateTilt() we check the ReadValue of the Drone Action Map, Tilt Action and store it as _tiltInput.

As stated earlier, the Vector2 values for each key is unique:

W: 0,1 | S: 0, -1 | A: -1, 0 | D: 1, 0

In the CalculateMovementUpdate() method, we are simply controlling the rotation.

Now, I would have named this method something with rotate in the name, but I’ve stuck fairly close to the original layout of the script being updated.

As we can see above, once again the ReadValue is key to determining the input.

Since this is a 1D Axis (Positive / Negative key assignment) and not a Vector2, we simply check if the ReadValue is < or > 0 to determine the active input.

The last method we need to look at is CalculateMovementFixedUpdate() which controls our Up/Down movements.

Like the rotational input, this is stored as a 1D Axis and thus we can determine the input keys in the same manner.

We wrap this in a .IsPressed() check to ensure that there is a value to evaluate.

In the next article I’ll look at the updated Input code for driving the forklift in this scene.

--

--