Cross-platform Input Controller

It is about time to turn the Dungeon Escape game into a mobile game. Up to now, we have been controlling the Player only with keyboard and mouse, by getting data using Input.GetAxis, GetKeyDown and GetMouseButtonDown. Since these lines has no compability with mobile, we need to transform the input controller into cross-platform so that we can control the Player both on PC and mobile.

As you can see below, I have updated Canvas with HUD that consists of joystick, buttons, health bar and diamond count.

Before going further, make sure that you are using the Unity Project on the correct platform. For this game, it will be build in Android. Make sure to switch to Android and click on Switch Platform to enable Unity to adjust the project accordingly.

Building your project, you can easily open it on your Android device by using Dropbox.

To enable the cross paltform input, we need to download Unity’s Standard Assets from the Asset Store. If you already added to your user id, you can easily reach it within the Package Manager. Simply click download.

Select the CrossPlatformInput folder and import into your project.

Then, drag and drop the MobileSingleStickControl Prefab into the project and adjust the buttons and Joystick with your own sprites of desire.

In order to use CrossPlatform input, we need to add it to the namespaces. Since the player takes the move commands within the Player script, add the following to the namespaces:

Now, we have access to the CrossPlatformInput library. Before using cross platform, we were moving the Player with the following line :

float horizontalInput = Input.GetAxisRaw(“Horizontal”);

Updating it with the following will enable us to control the player both on PC and mobile :

float horizontalInput = CrossPlatformInputManager.GetAxisRaw(“Horizontal”);

Since the MobileSingleStickControl has ready scripts, the buttons are called with the names provided in the inspector. In order to know that we are pressing the A or B Button, name the buttons from the inspector accordingly, so that we will call them within the Player script.

Name changed to : A_Button

And that’s it!

Unity Developer