Nerd For Tech
Published in

Nerd For Tech

Unity Guide

Cross platform input controller | Unity

A quick guide about how to implement mobile input in Unity

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

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

Top 5 Software Development Challenges and Solutions

Software Development Challenges and Solutions

Magic of Lambok in Java programming

Securing your Software Supply Chain with in-toto

Optimised Python Data Structures

GOmetalaunch Whitepaper Release

Kiali, a developer journey — Day 2: Please Kube, what can I do?

Customize a Fast Search with Fuzzy Finder for Vim

Competitive Programming: Progress and Insights as a Beginner

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fernando Alcantara Santana

Fernando Alcantara Santana

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

More from Medium

Using Decals in Unity’s HDRP

2.5D Platformer — Improving the Ledge Grab #10

Unity Feature 101: Basic Saving Using PlayerPrefs

Implementing a Common Scenario (Loot Chest) with Timeline in Unity 2020