Tutorial: Connecting your Xbox One / 360 gamepad to Unity

UPDATE, 10/24: PHL Collective, the authors of this article, release ClusterPuck 99 on Xbox One, as of 9/9/15.

The fine folks at PHL Collective, a Philadelphia based game development studio put together a brief tutorial on how to connect your Xbox One / 360 gamepad, either wired or wireless (using the receiver), to Unity.

word doc

Instructions as a word doc

 

Xbox One gamepad drivers

PC Drivers for Xbox One Controller (x86)

PC Drivers for Xbox One Controller (x64)

Installing the 360 wireless dongle

I’ve had to install the wireless dongle for the Xbox 360 gamepad before. Here’s how I did it.

  • Right click on Computer
  • Go to Properties
  • Click on Device Manager
  • Right click on the Unidentified Device
  • Go to Properties
  • Go to the Drive tab
  • Click on Update Driver…
  • Browse my for driver software
  • Let me pick from a list of device drivers on my computer
  • Microsoft Common Controller for Window Class
  • Xbox 360 Wireless Receiver for Windows
  • Update Driver Warning
  • Click Yes

Introduction

Adding controller support to a game can be very useful; many gamers feel more comfortable using controllers rather than a mouse and keyboard, and certain types of games work best when controllers are used. Using Unity and Xbox USB controllers, the process of implementing controller support is pretty straightforward. This tutorial will take you through the steps to add Xbox controller support for your Unity game.

Supplies

  • – Xbox controller: Either an Xbox 360 wired controller, an Xbox 360 wireless controller with a wireless USB dongle, or an Xbox One controller with a micro USB cable
  • – Unity3DGamepad
  • – A “can-do” attitude

 

Setting Up the Project

The first step to getting Xbox controllers working is to make sure the correct drivers are installed on your computer. On Windows, wired Xbox 360 controllers generally work out-of-the-box. Wireless Xbox controllers might require additional drivers to work, which come with the wireless USB dongle. Xbox One controllers require drivers which can be found on the Xbox support site: http://support.xbox.com/en-US/xbox-one/accessories/controller-pc-compatibility

In this tutorial, we will be adding controller support to a brand new Unity project. Start up Unity, and go to File > New Project.

 Setting Up the Input Manager

In order to set up Unity so we can easily access controller inputs from scripts, we need to properly set up the Input Manager. Go to Edit > Project Settings > Input to open the Input Manager.Input Manager

There are 20 total input buttons and axes on an Xbox controller, but for this demo we will only add the 4 face buttons, A, B, X, and Y, and the two joysticks. This is the most tedious part of the process, so if you’d like to skip it, I’ve provided a completed InputManager.asset file to download and use here:
LINK

Add 8 new axes to the Input Manager by changing the size value. The first 4 inputs will represent the 4 face buttons. Open the dropdown menu for the first new Axis, and change the values to the following settings for the A button:

Ais

For the next 3 inputs, input the same settings for the other 3 face buttons, except for “Name” and “Positive Button”. The button settings should reflect the values on the Unity3D Xbox Controller wiki:

http://wiki.unity3d.com/index.php?title=Xbox360Controller

 

NOTE: Windows, Mac, and Linux all have different button values for the Xbox controller, as seen on the Wiki page. To make the process of switching between these platforms easy, the InputManager.asset file found in the Project Settings folder in your Unity project folder stores your input settings. If you create a separate InputManager.asset file for each platform, and store them in another folder, you can switch them in and out by replacing the file in your Unity project folder with the one for the desired platform.

 

The last 4 inputs will represent the 2 axes on the 2 joysticks. For the first one, change the values for the following settings for the X axis on the left joystick:

Axis 2

 

Input the same values for the next 3 inputs, for the left joystick Y axis, and the right joystick X and Y axes. Again, refer to the Xbox controller page on the Unity3D Wiki to find the correct axis values:

http://wiki.unity3d.com/index.php?title=Xbox360Controller

 

 Setting Up a Scene

 

For this example we will create a simple scene to move a character around in. Add a plane with a collider as a floor, and add a capsule on top of it as a player. If the capsule has a capsule collider on it, remove the capsule collider and instead add a character controller component. Make sure there is a camera in the scene, and it is pointed at the objects in the scene. Finally, add a light into the scene so we can see our objects properly.

Setting up scene

 Coding the Player Movement

 

Next we will write a script to control our player. Create a new C# script called PlayerMovement, and open it in your editor of choice. Write the following code:

 

using UnityEngine;
using System.Collections;
 

public class PlayerMovement : MonoBehaviour

   {
      private Vector3 movementVector;
      private CharacterController characterController;
      private float movementSpeed = 8;
      private float jumpPower = 15;
      private float gravity = 40;

 
   void Start()
   {
      characterController = GetComponent<CharacterController>();
   }

 
   void Update()
   {
      movementVector.x = Input.GetAxis("LeftJoystickX") * movementSpeed;
      movementVector.z = Input.GetAxis("LeftJoystickY") * movementSpeed;

      if(characterController.isGrounded)
      {
      movementVector.y = 0;

      if(Input.GetButtonDown("A"))
      {
         movementVector.y = jumpPower;
      }
   }


   movementVector.y -= gravity * Time.deltaTime;

    characterController.Move(movementVector * Time.deltaTime);

   }

}

 

Finally, add this script as a component to your Capsule, and press play to test out your game.

 

 Adding Player Number 2

 

Adding support for multiple controllers is a great way to include local multiplayer in your game. To add more controller inputs, add more axes to the Input Manager. Repeat the same process as before, except when entering the “Positive Button” values, include the number of the joystick. For example, instead of “joystick button 0”, write “joystick 2 button 0”. Similarly, for the left and right joystick axes, change the “Joy Num” dropdown value to reflect the correct joystick number. Finally, when writing the “Name” value for the buttons and joystick axes, add a suffix which denotes the joystick or player number. For example, instead of “LeftJoystickX”, write “LeftJoystickX_P2”. Remember to change all of these settings for your first set of inputs as well.

Axis 4Axis 3

After changing all of the input settings, you will have to make some changes to the PlayerMovement script as well. Make the following changes (new or altered sections are written in bold):

 

using UnityEngine;
using System.Collections;

public class PlayerMovement : MonoBehaviour
{

   private Vector3 movementVector;
   private CharacterController characterController;
   private float movementSpeed = 8;
   private float jumpPower = 15;
   private float gravity = 40;
   public int joystickNumber;


   void Start()
   {
      characterController = GetComponent<CharacterController>();
   }


   void Update()
      {
      string joystickString = joystickNumber.ToString();

      movementVector.x = Input.GetAxis("LeftJoystickX_P" + joystickString) * movementSpeed;
                  movementVector.z = Input.GetAxis("LeftJoystickY_P" + joystickString) * movementSpeed;

      if(characterController.isGrounded)
      {
      movementVector.y = 0;

      if(Input.GetButtonDown("A_P" + joystickString))
      {
      movementVector.y = jumpPower;
      }
   }

   movementVector.y -= gravity * Time.deltaTime;
   characterController.Move(movementVector * Time.deltaTime);
   }
}

 

After changing the script, duplicate your player object, and move it to the side a bit, so it’s not overlapping the first player. Select the first player, and look at your PlayerMovement component in the inspector. Because we made joystickNumber a public integer, we should be able to change it directly in the inspector. Change one player’s joystickNumber to 1 in the inspector, and change the other player’s to 2. This should allow you to control each player separately with 2 controllers.

 

 Conclusion

 

Now that you’ve learned to set up basic controller support, you can use these skills for practically any game you make in Unity. This is a valuable skill to know for both allowing controllers for PC games, as well as for developing Unity games for consoles, like the Xbox. Take this knowledge with you, young game developer, and fly. Fly into the glorious future.

 

Might I suggest other ways to use your gamepad? PHL Collective’s newest game, Clusterpuck 99 supports up to 8 of them at once. I like to think of it as playing something similar to NHL 95′, but without having to buy that crappy EA adapter to have more than two gamepads attached to your genesis.

clusterpuck99-h

 

-----------------------


subscribe-to-youtube

16 thoughts on “Tutorial: Connecting your Xbox One / 360 gamepad to Unity

  1. Having this issue, “ArgumentException: Input Axis LeftJoystickX is not setup.
    To change the input settings use: Edit -> Project Settings -> Input
    CharacterMovement.Update () (at Assets/CharacterMovement.cs:19)”

  2. How can you set your controller to always go where you are looking beeing up always foward and avoid loosing north while I look somewhere else?

    • You should post this in the Unity forums. They have a very active community and I’m sure they can point you in the right direction.

      This could benefit others too, because I’m confident that other people have a similar question, so it could make it easier for them to locate the answer in the future.

  3. i donwnloaded the InputManager.asset but when i want to import it i have this message (unity 5,3,0)
    “You are trying to import an asset which contains a global game manager. This is not allowed”
    any toughts ?

    • No idea. I’ve never seen that error before. Can you find anything else that mentions an error about having global game manager?

  4. Have you solved Jesse Nance’s issue… He/She was having this issue , “ArgumentException: Input Axis LeftJoystickX is not setup.
    To change the input settings use: Edit -> Project Settings -> Input
    CharacterMovement.Update () (at Assets/CharacterMovement.cs:19)”

Leave a Reply to KohaiPanda Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.