Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to detect xbox controller

Discussion in 'Scripting' started by Leonard-Walker, Jul 5, 2015.

Thread Status:
Not open for further replies.
  1. Leonard-Walker

    Leonard-Walker

    Joined:
    Oct 31, 2012
    Posts:
    14
    How would I go about detecting if the user/player is using the keyboard and mouse or an xbox controller?
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Use Input.GetJoystickNames(), it will return a string array with the names of any gamepads.
     
  3. Leonard-Walker

    Leonard-Walker

    Joined:
    Oct 31, 2012
    Posts:
    14
    I tried using Input.GetJoystickNames() and it always says that I am using the controller, even if it is turned off.
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Weird, I have a wireless Xbox 360 controller, and it only shows up when it is turned on.
     
  5. Leonard-Walker

    Leonard-Walker

    Joined:
    Oct 31, 2012
    Posts:
    14
    Here is what I am currently using and it is providing problems.
    Code (JavaScript):
    1.  
    2. #pragma strict
    3.  
    4. function Start () {
    5.  
    6. }
    7.  
    8. function Update ()
    9. {
    10.     if(Input.GetJoystickNames() >= 0)
    11.     {
    12.         Debug.Log("Using Controller");
    13.     }
    14.     else
    15.     {
    16.         Debug.Log("Using Keyboard");
    17.     }
    18. }
     
  6. DeYuang

    DeYuang

    Joined:
    Sep 14, 2012
    Posts:
    12
    I struggled with this a while back too, and I used a different solution. Perhaps using GetJoystickNames() is better in your case, but here's what I did.
    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.Joystick1Button0) || Input.GetKeyDown(KeyCode.Joystick1Button1) || Input.GetKeyDown(KeyCode.Joystick1Button2) ||  et cetera){
    2.             print ("playing with a controller");
    3.         }
    4.         else if(Input.GetAxis("Mouse X") != 0F || Input.GetAxis("Mouse Y") != 0F || Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || et cetera){
    5.             print ("playing with mouse & keyboard");
    6.         }
     
  7. Rachan

    Rachan

    Joined:
    Dec 3, 2012
    Posts:
    663
    You may need to add more code by

    Code (CSharp):
    1. string[] joys = Input.GetJoystickNames();
    2.         int controllerCount = 0;
    3.         for (int i = 0; i < joys.Length; i++)
    4.         {
    5.             if (joys[i] != "")
    6.             {
    7.                 controllerCount += 1;
    8.             }
    9.         }
    10.         if (controllerCount > 0)
    11.         {
    12.             Debug.Log("Using Joystick");
    13.         }
    14.         else
    15.         {
    16.             Debug.Log("Not use Joystick");
    17.         }
    Because just use Input.GetJoystickNames().Length > 0
    is not enough and it will just empty text with no Joystick
    But it still count Length > 0
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,557
    Please check the dates on posts before replying. You're 7 years too late to reply here.

    Thanks.
     
Thread Status:
Not open for further replies.