It works everywhere! -A guide to setting up a cross-platform input controller using the Unity Input System

In this article, I will go over how to set up a character controller using the new Unity Input System and the Player Input Component which will work across Android and Windows.

NOTE: You will need to install the input actions package and change your event system and input system to follow this article — check out my other article on how to set this up.

Adding input actions

The first step we will take is to create our input actions asset. Right-click in the project folder and select “Create” > “Input Actions”.

Double click the controls asset to open it. Here we want to create a new action map for our player. Click the plus button under action maps and rename it.

I usually delete the bindings Unity provides by right-clicking them and selecting delete.

Next, we can create the actions. Click the “+” button on the Actions column, from here we will create our movement action, so rename the action to “Movement” and under the Properties change the Action type to “Value” and the Control type to “Vector 2”.

Now we will set the button binding, press the “+” button on the Movement action and select “Add 2D Vector Composite”. This will give you a 2D vector to set up. As I am creating a side scroller I do not need the up and down bindings so delete them by right-clicking and selecting delete. To set the left action to the “A” key under properties change the Path to “A [Keyboard]. You can press the listen to find the button. Do the same for the “D” button for the right binding.

Next, let's add an action for our jump and attack functions.

Click the “+” button under actions and add a new action, we will call this “Attack”. Change the Action type to “Button” and set the binding the button you wish to use (I will use the left mouse click). Do the same for your jump action. Don’t forget to save your asset.

Next, create a script to handle your player input (I called mine PlayerMovementController). We want to set up the functions to receive the context data from our input action events. In order to be able to access the interactions we need to use the namespace “UnityEngine.InputSystem”.

The function we will use for movement will read the value of the input and assign it to a float variable.

We can hook up our attack and jump functions by creating functions that read if the context data returns a value of performed. I have used a ground check utility I created to determine if the player is grounded before they jump. I created the ground check utility in another article.

I have moved the attack function onto its own script (called PlayerAttackController).

Next, add the scripts and “Player Input” component to our player.

To set up the player input component we need to add our input actions asset to the Actions variable, then switch the Behavior to “Invoke Unity Events”. We can now set up the events. Expand the “Events” and “Player” dropdowns.

We can now hook up each of our functions to an event. We do this by pressing the “+” button under each of our actions, adding our player game object to the event list then selecting the script and appropriate function. For example for movement, I selected my “PlayerMovementController” and added the “ReadXValue” script. Do this for the rest of your functions.

Adding cross-platform support

Next, we can add some cross-platform support. This will allow us to control our game on a mobile device.

I have a joystick, an A button and a B button image to use as our onscreen controls.

On the joystick we need to attach the “On Screen Stick component”, setting the “Path” variable to the “Left Stick Gamepad”. This doesn’t limit the control to a Gamepad it mealy reads the input from the on-screen joystick as though it is a Gamepad stick.

Next, add the Gamepad left stick as a binding to your movement action in the input actions asset.

Now we can set up the buttons. Add the component On-screen button to the A & B buttons. I set the path to the South and East buttons (Gamepad).

Now we can add those inputs to our input actions asset. We will use these actions to trigger our attacks and jumps.

Now, all we need to do is a test on functionality.

As you can see we can use the keyboard and the onscreen controls.

That's all for now.



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