Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Finding Joystick Mapping Inputs with Unity

Discussion in 'Editor & General Support' started by Arowx, Oct 9, 2013.

  1. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    6,921
    OK I got a new joystick and have been stumped on how to get the input mappings to work.

    Well I was going through the process of trying each axis in game, until I figured out a simpler way to do it, map all the 10 input axis for each joystick and write a simple program to display them.

    With the following code and some mind numbingly boring Ctrl+D edit and repeat work in the input manager I have found all the relevant buttons and axis for my joystick :D

    PS Unity please improve the InputManager to allow dynamic generation of "Axes" or if there is a way to do it via code please let me know.

    Anyway here is the amazing code that helped me out:

    Code (csharp):
    1.  
    2. // Arowx.com 2013 - free to use and improve!
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class JoystickTester : MonoBehaviour {
    7.  
    8.     public TextMesh joysticks;
    9.     public TextMesh[] inputText;
    10.     public TextMesh[] buttonText;
    11.  
    12.     public int numSticks;
    13.  
    14.     void Start()
    15.     {
    16.         int i = 0;
    17.  
    18.         string sticks = "Joysticks\n";
    19.  
    20.         foreach (string joyName in Input.GetJoystickNames())
    21.         {
    22.             sticks += i.ToString() + ":" + joyName + "\n";
    23.             i++;
    24.         }
    25.  
    26.         joysticks.text = sticks;
    27.  
    28.         numSticks = i;
    29.     }
    30.  
    31.     /*
    32.      * Read all axis of joystick inputs and display them for configuration purposes
    33.      * Requires the following input managers
    34.      *      Joy[N] Axis 1-9
    35.      *      Joy[N] Button 0-20
    36.      */
    37.     void Update () {
    38.  
    39.         for (int i = 1; i <= numSticks; i++)
    40.         {
    41.             string inputs = "Joystick " + i + "\n";
    42.  
    43.             string stick = "Joy " + i + " Axis ";
    44.  
    45.             for (int a = 1; a <= 10; a++)
    46.             {                
    47.                 inputs += "Axis "+ a +":" + Input.GetAxis(stick + a).ToString("0.00") + "\n";
    48.             }
    49.  
    50.             inputText[i - 1].text = inputs;
    51.         }
    52.  
    53.         string buttons = "Buttons 3\n";
    54.  
    55.         for (int b = 0; b <= 10; b++)
    56.         {
    57.             buttons += "Btn " + b + ":" + Input.GetButton("Joy 3 Button " + b) + "\n";
    58.         }
    59.  
    60.         buttonText[2].text = buttons;
    61.  
    62.     }
    63. }
    64.  
    So you need to create a new scene, add in a 3D TextMesh object link it to the joysticks field and run it to find out how may devices you have and what they are called.

    Add in the appropriate number of TextMesh objects for each device you want to test.

    The slow and arduous bit is adding input axes to the InputManager: :rolleyes:

    For joystick axes, create a Joystick Axis, with the appropriate axis and joy num, naming it Joy A Axis B, where A is the joystick number and B the axis number (note X axis = 1, Y = 2 then 3 to 10).

    For buttons just ensure the name follows the Joy A Button B pattern and the joy num match and set the type to key or mouse button and the positive button name = joystick button B.

    If anyone has a faster way to do this please let me know.:(:confused:

    Now run your joystick/input tester and work out which axis map to which inputs.:D

    Tada you will have the input axis and buttons mappings you need to use your controller.
     
    Last edited: Oct 9, 2013
    Bredjo likes this.
  2. Ferreteer

    Ferreteer

    Joined:
    Jan 5, 2014
    Posts:
    1
    So all that needs to be done is entering the axes for all possible buttons?
     
  3. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    6,921
    Yes, although I think it is possible to Automate this as I found a similar system for this on github that uses code to build up the InputManager entries and only works in editor mode.

    The only problem with that version was the fact that it cleared the existing default mouse/keyboard bindings.
     
    renman3000 likes this.
  4. Bredjo

    Bredjo

    Joined:
    Jul 25, 2017
    Posts:
    16
    @Arowx thank you very much :). Your script and solution helped me a lot :D.
    At first glance I didn't understand how the text output would look like. That's why I made a screenshots as an possible output example.
    Explanations:
    • text on top: name of the connected Controller
    • left column: assigned joystick buttons (input manager), get true when pressed
    • right column: assigned joystick axis (input manager), show value between -1 and 1 when used
    In the screenshot button 2 ("Btn 2:") was pressed which corresponds to the "X" button on the xBox One Controller in Windows. That means one knows now that the "X" button corresponds to the "joystick button 2" command in unity.
     

    Attached Files:

    Last edited: Mar 8, 2019