Cross-Platform Input Controller in Unity, part 2: coding the mobile controller.

Yesterday, we set up our mobile controller HUD, today we’re going to bring it into our existing code. Looking at the MobileJoystick gameobject, we can see it already uses the familiar Horizontal and Vertical axis we’re already using!

Here’s my current player movement code:

Right now we’re using the regular PC Input. We’ll just focus on the horizontal code for now for the rest of the example:

Since we installed the standard assets, we now have a cross-platform control library available to us which we’ll need to include in our namespaces:

using UnityStandardAssets.CrossPlatformInput;

Next, we’ll replace Input with CrossPlatformInputManager:

Now when we run our game, we can use either PC or Android controls! How cool is that?

The sword attack is button down, so while I’m selecting the android joypad, it’s also attacking so everything IS working as intended! Awesome!

Now that we have movement down, let’s tackle the jump button. If we look at our b button handler script, it already has the standard Jump tag attached, we can take advantage of that!

We’ll just add the CrossPlatformInput check along with our regular button check:

So we’ll surround our first check of Input and CrossPlatform input with a pair of parenthesis to separate it from the other part of the logic to check if the player is grounded, but now we should have a working jump button. let’s check!

Huzzah! Spacebar and B button are working as intended!

Lastly, let’s do the Attack key which will be button A. We’ll have it conform to Unitys input manager standards and have the Button handlers script name input be “Fire1”

So now we’re using Fire1 to check our pc button down as well as the cross platform button. For now, I’m going to not use the Input.GetButtonDown to check if it works, if it does, then I’ll put it back in.

Excellent! the button is working!

Note: Now that we’re using the cross-platform input, I noticed the left and right keys stopped working on the pc. Switching platforms from android to linux/pc fixed the issue which means we really only truly need to rely on CrossPlatformInputManager.

cartoonist, game artist, and wanabe gamedev.