How to Use Xbox Controller in Unity

Unity Engine, a robust platform for game development, offers comprehensive tools for integrating various input devices. Among the widely used devices, the Xbox controller stands out for its ergonomic design and extensive functionalities. This tutorial will elucidate the integration of an Xbox controller into Unity projects, detailing the process of accessing input data and answering common queries related to this integration.

1. Setting Up the Development Environment

  • Ensure that the latest version of Unity is installed.
  • Connect the Xbox controller to the computer. Ensure that drivers are up to date and that the system recognizes the controller.

2. Accessing Input Manager

To modify and view the current inputs:

  • Open Unity.
  • Navigate to 'Edit' -> 'Project Settings' -> 'Input Manager'.
  • In the Input Manager, multiple axes and buttons are listed. For Xbox controllers, certain default axes and buttons like "Horizontal" and "Vertical" correspond to the joystick movements.

3. Configuring the Xbox Controller Inputs

For the purpose of this tutorial, let's set up the 'A' button and the left 'Joystick'.

  • Expand the 'Axes' in the Input Manager.
  • To add a new input, right-click on any existing axis and duplicate it. Rename this to "A_Button".

A_Button Configuration:

  • Set the 'Type' to 'Joystick Axis'.
  • Choose the correct 'Axis' for the A button. For Xbox controllers, this is typically the 0th axis.
  • For 'Joy Num', choose 'Get Motion from all Joysticks' unless there's a specific joystick intended for use.

Left Joystick Configuration:

  • Duplicate another axis and rename it "JoystickHorizontal" for the horizontal movement. Set 'Type' to 'Joystick Axis'. Set 'Axis' to 'X axis'.
  • Duplicate once more and rename it "JoystickVertical" for the vertical movement. Set 'Type' to 'Joystick Axis'. Set 'Axis' to 'Y axis'.

4. Accessing Controller Input in Scripts

using UnityEngine;

public class XboxControllerInput : MonoBehaviour
{
    void Update()
    {
        float horizontal = Input.GetAxis("JoystickHorizontal");
        float vertical = Input.GetAxis("JoystickVertical");
        bool aButtonPressed = Input.GetButtonDown("A_Button");

        // Implement desired actions based on the input values
    }
}
  • Attach this script to any relevant GameObject to retrieve input values and manipulate them as required.

Answers to Common Questions:

  1. Why aren't the inputs from the Xbox controller being recognized in Unity?: The Xbox controller might not be connected properly or the drivers might not be updated. Ensure that the system recognizes the controller. Additionally, verify that the Input Manager settings correspond to the correct joystick number and axis.
  2. How to differentiate between multiple connected Xbox controllers?: In the Input Manager, 'Joy Num' allows the selection of a specific joystick number. Assign each Xbox controller a unique number to differentiate between them.
  3. Can custom button mappings be made for the Xbox controller in Unity?: Absolutely. The Input Manager facilitates custom button mappings. Just duplicate an existing axis/button, rename it, and adjust settings accordingly.

Conclusion

Integrating the Xbox controller into Unity projects enriches gameplay experiences, granting players familiar and intuitive controls. By understanding the Input Manager and the Input class, a wide array of controller functionalities can be seamlessly incorporated into any Unity game.

Suggested Articles
How to Use New HDRP Water System in Unity
How to Set Up Joystick Controller for Movement in Unity
Zone Controller Pro - Unity Asset Store Package
Script for Creating a Light Switch in Unity
Top Unity Assets from the Asset Store
Unity FPS Counter
Unity Capture Screenshot Tutorial