Journal 109 — Unity 2D Mobile, Dungeon Escape Android Input System

Objective: To set up the touchscreen inputs for movement, jump and attack

In the previous article, I added a first build of the game to an Android device, and created the HUD for the life system, movement button, and attack and jump buttons. Now we want to use the Unity 2021 Input System to add the touchscreen functionality.

Input System Package

We first need to add the Input System package from the Package Manager. You may see a dialog box pop up to convert from the old input system to the new Input System, so select yes. You can also update this in the Player Project Settings.

Input Action Asset

After this is done we can add a new Input Action asset called “PlayerControls” to store the Actions along with various controls for devices such as keyboard, mouse, and touchscreen. We can open up the Action Asset window and start filling out the controls.

I’ll start with an Action Map named “PlayerMain”, which will be the entire collection of Actions, and I’ll call the Actions “Movement”, “Jump”, and “Attack”. We need to now set up the following:


—WASD movement with “A” and “D” keys or left and right arrow keys

— Jump movement with the Spacebar key


— Attack button with the left click mouse button

Touchscreen: In this example for Android, use a Gamepad in case one is connected to the phone. You can assign the Left Stick for movement, and “A” and “B” buttons to tap for jump and attack.

To help with organization, I created two Control Schemes, one called Touchscreen and one called Keyboard Mouse.

Be sure to select Save Asset, and then you can select Generate C# Class and hit Apply to generate the C# script for the controls.

Onscreen Controls

We had previously added the onscreen control icons for movement and jump and attack. We can add an On-Screen Stick and On-Screen Button component to handle the touch interface when playing on mobile. Note the Control Path for movement and jump and attack need to match the controls used in the Input Action asset.

The last update in the Hierarchy is to update the Event System to make sure the new Input System is used. The default values can be left alone, but in my case to use both the PC input and also the touchscreen input, I switched the Point Behavior to “All Pointers As Is”.

Player Control Updates

Now we need to access the PlayerControls Input Action script to use the new input system.

In the Player C# script, we need to do the following:

  • Add a reference to the PlayerControls Input Action
  • Set an OnEnable and OnDisable function to listen for the PlayerControls Input Action
  • Update the movement, attack, and jump functions to the new input system

We can start with a variable for the PlayerControls Input Action asset, and use the Awake function to create a new instance of this Input Action. Then we need to enable and disable the Input Action. We can do this in the OnEnable() and OnDisable() functions.

The last updates to the script are to change out the old input system (e.g., Input.GetKeyDown…) with the new Input System.

To check for the attack, use the following:

As you can see for a simple button action, the command is accessed starting with the Input Action, then the Action Map, then the Actions.

To check for the jump, use the following:

Last, to check for movement, use the following:

We can check the results on a PC and see if the keyboard and mouse work and also the on-screen controls work.

It looks like there is a glitch with the on-screen controls but that is because the mouse click when interacting with the on-screen controls also performs an attack.

Let’s try this now in an Android build. And it works!

Thank you for your time!

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.