Day 126 — Cross Platform Input Controller

Connor Fullarton
3 min readJul 19, 2021

--

Hey and welcome!

In this one we’re going to look into giving our controls some cross platform functionality so that we can play this game on a mobile android device.

To start things off you need to import the Unity Standard Assets pack into your project. Head over to the asset store which can be found in the Window menu if you don’t have it already and search for Standard Assets and you should be able to find this:

Click on the Add to My Assets option and then choose the Open in Unity option which should load up these assets in your package manager allowing you to download and then import them:

If you’re using a newer version of Unity you’ll most likely get an error in the console about GUIText being obsolete. This is a nice easy fix, double click the error to bring you directly to the script or open the script up manually from here Assets\Standard Assets\Utility\SimpleActivatorMenu.cs.

public GUIText camSwitchButton;

Change the above line to the one below:

public Text camSwitchButton;

Then make sure you include this as a dependency since we’re working with the UI:

using UnityEngine.UI;

With that done, head on over to Standard Assets > CrossPlatformInput > Prefabs and drag and drop the MobileSingleStickControl prefab into the hierarchy. This is going to give you a joystick in the bottom left corner that you can move about within a set range. The current range is set to 100 and if you wish to shorten or increase that range then you can do that in the Joystick component that’s in the MobileJoystick object that’s a child of the prefab you dragged in.

While you’re in here, change the Source_Image to Thumb_Button by searching for it or use your own custom image and you should have something like this when you run the game now:

Once you’ve done that go ahead and duplicate the jump button object and rename these to A_Button and B_Button. Search for the A and B button images to assign as the source image and click on the preserve aspect option so that the images even out. Scale and adjust to your liking and delete the button text and you should have something like this now:

When you’re happy with that go ahead and make some edits to your code in your Player script:

using UnityStandardAssets.CrossPlatformInput;void Update()
{
if (CrossPlatformInputManager.GetButtonDown("A_Button") && IsGrounded() == true)
{
_playerAnim.Attack();
}
}
void Movement()
{
float move = CrossPlatformInputManager.GetAxis("Horizontal");
_grounded = IsGrounded();
if (move > 0)
{
Flip(true);
}
else if (move < 0)
{
Flip(false);
}
if (CrossPlatformInputManager.GetButtonDown("B_Button") && IsGrounded() == true)
{
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x,
_jumpSpeed);
_playerAnim.Jump(true);
StartCoroutine(ResetJumpRoutine());
}
_rigidbody.velocity = new Vector2(move * _movementSpeed, _rigidbody.velocity.y);
_playerAnim.Move(move);
}

What’s happening here is that we’re including the CrossPlatformInput dependency and using it’s manager to bind the controls for our Attack, Movement and jumping to the joystick and buttons that we’ve added in.

Lastly head over to your A and B buttons and look for the Button Handler component. Change the name to A_Button and B_Button respectively and your on screen controls should now be operational and usable across any device/platform!

--

--

Connor Fullarton
Connor Fullarton

Written by Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

No responses yet