Adding Xbox controller support and input to your Unity3d game

 

xboxoc

Xbox One Wireless Controller can work with your PC if you download one of the following driver packages:

Note To use your Xbox One controller with your console after using it with a PC, you must re-sync the controller to the console. You can do this by using the wireless sync button or a USB cable. For details, see Connect a wireless Xbox One controller to your console.

If you want to use an Xbox 360 Controller with your computer, you can use an Xbox 360 Controller for Windows. Or, you can use an Xbox 360 Wireless Controller together with an Xbox 360 Wireless Gaming Receiver for Windows.

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.

inputmanager

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:

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:

axis

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:

https://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:

joystick

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:

https://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.

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:

    1: using UnityEngine;
    2:  
    3: using System.Collections;
    4:  
    5: public class PlayerMovement : MonoBehaviour
    6:  
    7: {
    8:  
    9: private Vector3 movementVector;
   10:  
   11: private CharacterController characterController;
   12:  
   13: private float movementSpeed = 8;
   14:  
   15: private float jumpPower = 15;
   16:  
   17: private float gravity = 40;
   18:  
   19: void Start()
   20:  
   21: {
   22:  
   23: characterController = GetComponent<CharacterController>();
   24:  
   25: }
   26:  
   27: void Update()
   28:  
   29: {
   30:  
   31: movementVector.x = Input.GetAxis("LeftJoystickX") * movementSpeed;
   32:  
   33: movementVector.z = Input.GetAxis("LeftJoystickY") * movementSpeed;
   34:  
   35: if(characterController.isGrounded)
   36:  
   37: {
   38:  
   39: movementVector.y = 0;
   40:  
   41: if(Input.GetButtonDown("A"))
   42:  
   43: {
   44:  
   45: movementVector.y = jumpPower;
   46:  
   47: }
   48:  
   49: }
   50:  
   51: movementVector.y -= gravity * Time.deltaTime;
   52:  
   53: characterController.Move(movementVector * Time.deltaTime);
   54:  
   55: }
   56:  
   57: }

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.

2joys

After changing all of the input settings, you will have to make some changes to the PlayerMovement script as well. Make the following changes.

    1: using UnityEngine;
    2:  
    3: using System.Collections;
    4:  
    5: public class PlayerMovement : MonoBehaviour
    6:  
    7: {
    8:  
    9: private Vector3 movementVector;
   10:  
   11: private CharacterController characterController;
   12:  
   13: private float movementSpeed = 8;
   14:  
   15: private float jumpPower = 15;
   16:  
   17: private float gravity = 40;
   18:  
   19: public int joystickNumber;
   20:  
   21: void Start()
   22:  
   23: {
   24:  
   25: characterController = GetComponent<CharacterController>();
   26:  
   27: }
   28:  
   29: void Update()
   30:  
   31: {
   32:  
   33: string joystickString = joystickNumber.ToString();
   34:  
   35: movementVector.x = Input.GetAxis("LeftJoystickX_P" + joystickString) * movementSpeed;
   36:  
   37: movementVector.z = Input.GetAxis("LeftJoystickY_P" + joystickString) * movementSpeed;
   38:  
   39: if(characterController.isGrounded)
   40:  
   41: {
   42:  
   43: movementVector.y = 0;
   44:  
   45: if(Input.GetButtonDown("A_P" + joystickString))
   46:  
   47: {
   48:  
   49: movementVector.y = jumpPower;
   50:  
   51: }
   52:  
   53: }
   54:  
   55: movementVector.y -= gravity * Time.deltaTime;
   56:  
   57: characterController.Move(movementVector * Time.deltaTime);
   58:  
   59: }
   60:  
   61: }

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.