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. Dismiss Notice

Read gamepad input using keycodes

Discussion in 'Scripting' started by sangolemango, Dec 22, 2019.

  1. sangolemango

    sangolemango

    Joined:
    Jul 22, 2018
    Posts:
    7
    Is there a way to read gamepad input (right trigger, square button, etc) using keycodes similar to reading keyboard and mouse input using GetKey()

    I want to do it this way so I can set gamepad buttons in a settings menu or something instead of having it predefined using the old and new input system.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Yes there is, but you need to do the heavy lifting yourself: create a UI for it, interactivity, etc. and make it just like your favorite control remapper screen in your favorite game.

    There may be assets on the asset store that will do this too. I remember hearing there was one called Rewired that did a lot of the hard work, but I have never used it or inspected it personally.
     
  3. sangolemango

    sangolemango

    Joined:
    Jul 22, 2018
    Posts:
    7
    For now I just want to set the gamepad buttons using enums so I can set them in the inspector. Is there a way to listen to gamepad inputs? that way I can detect if a certain button is pressed depending on the enums.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    If you're using the old Input system that requires strings for each axis, but you can make equivalently-named enums (such as
    Horizontal
    ,
    Vertical
    ,
    Fire1
    , etc) and then when you use them to actually GET the axis, you just need to add
    .ToString()


    Code (csharp):
    1. public enum MyInputEnums { Horizontal, Vertical };
    2.  
    3. public MyInputEnum LateralControl;
    Then the code to read that axis becomes:

    Code (csharp):
    1. var lateral = Input.GetAxis( LateralControl.ToString());
    If you're using the new input system I can't help you; haven't even looked at it yet.