Cross-platform Input Controller in Unity

Input functionality across PC and Mobile made easy!

Unity has a free standard asset pack available in the asset store that contains cross-platform input functionality that we can all use.

It’s called “Standard Assets.” Once downloaded, import only the “CrossplatformInput” and “Editor” folders.

In the CrossPlatformInput folder, there are prefabs available to us on the go. Since I am currently developing a 2D mobile platform game, I will drag the “MobileSingleStickControl” prefab to the Hierarchy.

The prefab will automatically place UI elements for the game. A joystick and a button. I changed the images to them and added a second button. When hitting the play button, they are intractable.

For the buttons, I made sure through the inspector to have a name for them. Here’s an example of the A button:

I named it Attack. I will then call this “Attack” through code.

In the player script, where I have my movement logic, I added using UnityStandardAssets.CrossPlatformInput; as a namespace, and I replaced:

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

to:

float xMove = CrossPlatformInputManager.GetAxis(“Horizontal”);

The GetAxis for the CrossPlatformInputManager will provide movement behavior for the computer and mobile devices.

I can now replace all the input to use CrossPlatformInputManager or add them with the regular input that is already there.

The B button will be for jumping:

if (Input.GetKeyDown(KeyCode.Space) || CrossPlatformInputManager.GetButtonDown(“Jump”) && _isGrounded)
{
_enableJump = true;
}

The A button will be for attacking:

if (Input.GetKeyDown(KeyCode.Mouse0) || CrossPlatformInputManager.GetButtonDown(“Attack”) && _isGrounded && !_isAttacking)
{
StartCoroutine(AttackRoutine());
}

With all of this set, I now have a working input system for mobile!

Gabriel

--

--

--

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Software architecture with the “IDesign method”

Day 15 : Create an AR Weather app using Unity, AR Foundation and OpenWeatherMap

Battleship: Building the Server

I am very satisfied with Testnet experience on Strip Finance .

Writing Scalable Authentication in Golang Using Go-Guardian

Top 10 Static Site Generators in 2020

Evolving Search for Product Availability

Updating dependencies in Azure DevOps repos

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
Gabriel Perez

Gabriel Perez

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.

More from Medium

Damage VFX using Animated Sprites in Unity

2D Mobile: Loot System

2D Homing Missile in Unity

Light layers in Unity HDRP