2D Mobile Game: Android Input Manager

Ryan McCoach
5 min readJun 4, 2022

--

Intro

In this article, we will cover how to add touch screen controls for Android implementation.

Cross Platform Input Setup

In the build settings, we will need to convert the project to an Android build which you can easily do using Unity.

Make sure to Install the Android module.

Once the Android module is installed, you can now switch the build to the Android platform.

After the build has been switched to Android, the first thing to do is adjust the screen dimensions to 16:9 (Landscape) as this game will be played with a phone that is horizontal.

Unity has Mobile Joystick and Button prefabs that can be used with touch screens.

In the Asset store, you want to import the Standard Assets for Unit into the project.

Once the Standard Assets for Unity has been imported, drag the Mobile Single Stick Control prefab into the game scene.

Android Inputs

The prefab has an interactive joystick and button that can be added Canvas. They will be used for the new inputs to control the player.

We will disable the joystick image that was used as a placeholder but use it as a reference for positioning.

For the Mobile Joystick in the prefab, we replace the Source Image with the our Joystick image and adjust the size to match the dimensions of the image. Also, decrease the Alpha in the Color to make it more transparent.

We can test the joystick movement, by clicking and dragging it.

The Mobile Joystick has properties that be adjusted like the movement range, which will limit how far it moves in all directions. We want to set it to a value that allows the joystick to move in all directions without leaving the scene.

We are going to be following a similar process for the buttons.

We will disable the button images we previous had as place holders and use them a position references.

For the interactive button, we replace the Source Image with our A Button image, scale it up, reduce the Alpha and preserve the aspect ratio.

For the B button we can just duplicate the A button and replace the Source Image with the B Button image and adjust the position.

The interactive buttons are setup and are now ready to be scripted.

Android Mobile Touch Control

In the Mobile Joystick prefab, the script has several properties and one of them is a reference to vertical and horizontal inputs. The image highlights the Vertical axis, but we are actually ignore that axis and only use the Horizontal axis for the left and right movement of the player.

In order to access these properties from the joy stick game object, we need to use the UnityStandardAssets.CrossPlatformInput library in the player script.

The Movement method stores the input value return from the Horizontal axis in a float. We previously were using the input system for a computer, but now want to convert it to use the CrossPlatrformInputManager.

The idea is the same, but now we are getting an input value from the joystick when it is moved horizontally (left and right). Lets comment out the old input code.

We have the joystick working, now onto the buttons.

For each of the buttons, the script allows you to give a name to the button.

This will help us identify which one is being pressed.

Back in the player script, we use the CrossPlatformInputManager and get the button down by using the name of the button.

We do this in each part of the code that requires the button press.

A button = Attack

B button = Jump

One thing you want to watch out for is the buttons are not overlapping each other to the point where one of the buttons can detect it being pressed. Space the buttons out enough so each can be pressed.

Unity makes it easy to convert the input from one platform to another.

Until next time…cheers!

--

--

No responses yet