Implementing mobile input for the game

One last step to conclude our game is to make it mobile friendly. So far, we can move the character only with a keyboard, but what about mobile devices with a touchscreen instead? If you remember the controls from Chapter 10, Let's Get Physical, we implemented the Movement Component independently from the input. As a result, the Player Controller script is responsible for gathering the input from the player to send it to the character. Also, we made the Player Controller inactive in the case of mobile devices, which is great. However, now it's time to implement the controller for the touchscreen. This will compile when the game runs neither in the Editor or in a standalone version.

To implement the system, we will use the UI system in Unity, which is already suitable for gathering input from touchscreens. Let's start by creating a UI image by right-clicking on the Hierarchy panel and then on UI/Image. We can use the graphics provided in our package. In this case, the graphic that we need looks like a triangular button. Using the knowledge we learned from Chapter 11, Don't Forget to Save!, let's scale it to the right size and place it well in the UI. Then, duplicate it and place another arrow next to it. Finally, use the up arrow from the other side of the screen, like in the following screenshot:

Wonderful! Now we can make them interactive with a bit of scripting.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset