Adding Gamepad Support to Unity Games on BlackBerry 10

Game Development

At the Unite Nordic and Nordic Gaming Conference, we showcased how a BlackBerry 10 smartphone can so easily turn into a console gaming system with your favorite gamepad and a micro HDMI cable hooked up to a TV. Unity developers loved it and wondered how their games can leverage that. This blog posts shows you how easy it is to add gamepad support for BlackBerry 10.

Background

The BlackBerry 10 Native SDK provides a native Gamepad API that currently supports the following gamepads

  1. Moga Pro Game controller
  2. SteelSeries Free controller
  3. Gametel controller
  4. Nintendo Wii Remote

Support for more gamepads are being added as we speak and native games utilizing this API does not have to worry about changing their code to support them.

The Unity SDK uses the Gamepad API to provide out-of-the-box support to the gamepads listed above through its Input system API. The API allows accessing the connected gamepad’s names, axis values and currently pressed buttons.

Lets see how to setup and leverage BlackBerry 10’s gamepad support in your Unity games.

Step 1: Configure gamepad’s analog joysticks

The first thing you need to do is setup the Input settings from the Project Settings as shown below:

TITLE_IMAGE

If your game would benefit from analog joysticks, the setup involves specifying names to axes of analog joysticks on your gamepad. For example, a top down shooter game with one analog joystick controlling the player movement and the other controlling direction of fire would require 4 axes to be setup:

Axis 1 – corresponds to the horizontal movements of the right joystick (X axis)

Axis 2 – corresponds to the vertical movements of the right joystick (Y axis)

Axis 3 – corresponds to the horizontal movements of the left joystick (3rd axis)

Axis 4 – corresponsds to the vertical movements of the left joystick (4th axis)

The Input settings below shows a sample configuration:

unity5

The cool part is that 2 joysticks can be paired to a BlackBerry 10 smartphone at once. If your game supports on-device multi-player feature, you could simply start by adding another 4 axes with Joy Num pointing to Joystick 2.

Step 2: Configure Gamepad Buttons

Actually, there is no configuration required here. The gamepad keys are already pre-mapped to KeyCode that your game could start listening to and react on. For example: the KeyCode mapping of a Moga Pro and SteelSeries Free Gamepad that are currently supported for BlackBerry 10 are shown below:

unity6

unity7

As you can see from the above mappings, other gamepads with conventional layouts will report the same KeyCode for a button in same/similar position. For example, pressing up on a DPad up should report JoystickButton8 on any gamepad with a DPad.

Step 3: Reading the Gamepad Input

The following C# script shows how straightforward it is to integrate the gamepad support:

using UnityEngine;
using System.Collections;
using System;

public class PlayerJoystickClass : MonoBehaviour {

          private Transform originalTransform;
          private string currentButton;
          private float[] axisInput = new float[4];

          // Use this for initialization
          void Start () {
                   for(int i = 0; i < axisInput.Length; i++)
                             axisInput[i] = 0.0f;

          }

          // Update is called once per frame
          void Update () {

                   // Get the Gamepad Analog stick’s axis data
                   axisInput[0] = Input.GetAxisRaw("Axis 1");
                   axisInput[1] = Input.GetAxisRaw("Axis 2");
                   axisInput[2] = Input.GetAxisRaw("Axis 3");
                   axisInput[3] = Input.GetAxisRaw("Axis 4");

                   // Get the currently pressed Gamepad Button name
                   var values = Enum.GetValues(typeof(KeyCode));
                   for(int x = 0; x < values.Length; x++) {
                             if(Input.GetKeyDown((KeyCode)values.GetValue(x))){
                                      currentButton = values.GetValue(x).ToString();
                             }
                   }

// Transform the object.
                   transform.Translate(0, 0, axisInput[1] * 0.05f);
                   transform.Rotate(0, 0, axisInput[2]*3);

if (currentButton.CompareTo("Joystick1Button0"))
                   {
                             // Fire something
                   }
          }

// Show some data
          void OnGUI()
          {
                   GUI.TextArea(new Rect(0, 0, 250, 40), "Current Button : " + currentButton);

                   GUI.TextArea(new Rect(0, 50, 250, 40), "Axis 1 : " +  axisInput[0]);
                   GUI.TextArea(new Rect(0, 100, 250, 40), "Axis 2 : " +  axisInput[1]);
                   GUI.TextArea(new Rect(0, 150, 250, 40), "Axis 3 : " +  axisInput[2]);
                   GUI.TextArea(new Rect(0, 200, 250, 40), "Axis 4 : " +  axisInput[3]);    
          }
}

First, we declare an array of floats to listen to 4 axes input values from the analog joysticks and a string to identify currently pressed button. To obtain an axis value, I simply call the Input.GetAxisRaw(<Axis name>) and then use it to transform my object. The currently pressed button can be obtained by simply checking for the KeyCode corresponding to the above gamepad mapping.

Note: Many gamepads still do not have support for analog joysticks (for example: Wii Remote, Gametel etc.) and it is worthwhile adding core game controls via DPad if relevant.

unity8

Step 4: Switching between touch control and gamepad

Almost all games have touch controls but with a gamepad in reach gamers would love to player your game with it instead. Those with an appetite for long and non-stop hours of gameplay would very much appreciate an on-the-fly switch from touch controls to gamepad. Assuming your Bluetooth is turned on and the gamepad is in the pairing mode, you should be able to detect a gamepad by simply checking Input.GetJoystickNames() in your script’s update() for “BlackBerry Gamepad”. When paired, you could disable the touch gamepad altogether and use real gamepad controls to put your game in a “console mode”.

Note: “BlackBerry Gamepad” is the currently returned identifier for all BlackBerry supported gamepads in BlackBerry 10 Add-on Open Beta. I recommend checking again for any changes in the returned gamepad identifier after Full release of the Unity BlackBerry 10 Add-on.

There you have it folks. This is all it takes to add gamepad support to your Unity games. Awesome thing is we are committed to adding support all the cool gamepads out there and you may never have to change a line of code to support them!

For any questions, comments, concerns or kudos feel free to reach out to me directly at rmadhavan@blackberry.com or connect directly on Twitter at @rmadhavan7.

About Ramprasad Madhavan

Ram was a Senior Gaming Consultant at BlackBerry's Developer Relations Team. With years of Advanced Graphics and Gaming Experience, he managed technical partnerships with Industry's Top notch Game Engines and Middleware partners. Indie Gaming has always been his passion and he strives to make available all the tools necessary for Indie Game developers be successful on new BlackBerry Platform. Prior to joining Developer Relations team, Ram was part of Gaming R&D team at BlackBerry where he helped design and develop the open-source gaming framework, Gameplay3D.

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus