Until now I’ve been working inside the Unity editor, so mainly to test the game I used a mouse and keyboard.
But I’m developing a game for smartphones so it is the case to start to insert commands that can be used on a mobile device.
To do this the fastest way is to use the standard Unity Assets that already include a number of different prefabs for different situations and also one for the management of touch controls on smartphones.
First, you have to download the standard assets from the asset store or package manager. After this step, we look for the folder CrossPlatformInput that we will find under the StandardAsset folder that we have just imported.
In the folder prefab, we will find everything we need, that is “MobileSingleStickControl” he is our prefab, now we just need some tweaks to make it unique and relevant to the game.
First, let’s change the appearance and insert the sprite of our joystick and our buttons. Now moving on to the inspector we can notice that there are a series of parameters that we can modify as the radius of movement of the joystick rather than the name in a string that we will recall in the code.
Let’s move on to the code, we enter in our Player class, and as the first thing, we add the namespace of the library that we have to use, then under the others, we add “using UnityStandardAssets.CrossPlatformInput;”
Now that we have access to all the methods of the CrossPlatformInput class let’s go edit the keyboard commands as in the image. In the case of the buttons, I kept the keyboard commands otherwise it would be complicated to test the functionality of the game having to use the pointer to press the keys on the screen.
And now we have our commands to use on mobile devices.