Unity Guide

Cross platform input controller | Unity

A quick guide about how to implement mobile input in Unity

Fernando Alcantara Santana
Nerd For Tech
Published in
5 min readNov 24, 2021

--

Objective: Implement cross platform input in our game to work on mobile with the new input system in Unity.

In the last post I covered how to work with OnClick events in Unity. Now, it’s time to use the new input system in Unity to implement a way for mobile input to work in our platformer game.

Before starting

If you’ve been following my previous posts, then you’ll remember that I implemented the movement and other mechanics using the new input system in Unity. I truly recommend to check these posts before starting this post in case that you don’t know how to use the new input system:

Current input actions

In the current input actions asset we contain 3 different actions:

  • Movement

This action is bonded with a 1D axis value to indicate if he player should move to the left or to the right.

  • Jump

This action indicates when the player should jump and it’s bonded with the space key.

  • Attack

This action indicates when the player should attack and it’s bonded with the left click from the mouse.

As you can see in the top left corner, the actions are bonded to the Keyboard control scheme. But what if we want our game to receive input from a mobile screen?… It’s really easy, we just need to add a new control scheme for the mobile input:

New control scheme

Once we add the Touch control scheme we’ll be able to see the same 3 actions that we have with the Keyboard scheme, and, as our player will only need to move left or right, we can bind a new 1D axis value to the movement action:

Once the value is bonded, let’s select the respective axis to use it as our input. As in many mobile games, we can choose to select the default left stick of a gamepad and the left or right direction according to the 1D axis values:

This way we’ll be able to move the player to the left when the gamepad stick goes to the left or when the A key is pressed on a keyboard.

Note: Don’t forget to click the Save Asset button after selecting the input path.

Adding the mobile input

And now, in order to use the mobile input, let’s add the respective sprites into the canvas. To move we’ll use a left stick and to jump and attack we’ll use two buttons at the right:

Once the sprites are added, let’s select the left stick image and attach an On-Screen Stick component that will provide it a stick behavior with the option to assign its movement range:

Finally, in order to connect the stick input with the new Touch control scheme, let’s select the respective control path (Gamepad’s left stick) through the inspector. And then, If we run the game, we’ll be able to drag the stick as in a mobile device according to the movement range value that we choose:

Once the respective control path is selected, let’s run the game and we’ll be able to see that the mobile input works as expected:

Extra (buttons)

If we want to implement the buttons we’ll just have to follow the same process for the other actions in the input actions asset. Just select the respective control path to bond with the action:

And then, in order to connect the input, attach an On-Screen Button component to the button images and select the same control path from above:

And that’s it, we used the new input system to implement mobile controls with Unity! :D. I’ll see you in the next post, where I’ll be showing how to implement mobile ads for our game with Unity.

If you want to know more about me, feel free to connect with me on LinkedIn or visit my website :D

--

--

Fernando Alcantara Santana
Nerd For Tech

A passionate computer technology engineer and Unity developer that is always looking to grow in every aspect of life :).