Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Xbox controller triggers?

Discussion in 'Editor & General Support' started by newjerseyrunner, Nov 29, 2018.

  1. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    Hello,
    I have a very strange issue. Unity doesn't seem to see my Xbox controller's triggers. I can interact with every other button and joystick on the controller though. For example, the left and right bumpers are "joystick button 8" and "joystick button 7." The joysticks are 1 and 3 yadda yadda yadda.

    I discovered these by simply opening a built game, going to the input tab and pressing the button and seeing what showed up. Nothing shows up when I press the triggers. I've tried both a "joystick axis" and "button," but it just never shows up.

    It's not the controller because I use it on my XBone almost every day, and I also play games with it on my Mac, so I know it's not the OSX driver.

    How do I get input from these triggers?
     
  2. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    The X Box 360 controller triggers share joystick axis 3. One trigger gives a negative and the other a positive value.
    The X Box One controller, I believe use joystick axis 9 and 10.
     
  3. GuirieSanchez

    GuirieSanchez

    Joined:
    Oct 12, 2021
    Posts:
    406
    I can't get them to work either. Could you guys provide a screenshot?
     
  4. LloydBurton

    LloydBurton

    Joined:
    Sep 26, 2018
    Posts:
    1
    Triggers use GetAxis not GetButton returning 0 or 1
    eg:
    if (Input.GetAxis("Fire1") == 1)
     
    Zayniac_Games likes this.
  5. gabriel-diquinzio

    gabriel-diquinzio

    Joined:
    Nov 3, 2015
    Posts:
    3
    i made this simple code if you'd like to use the input as a button
    Code (CSharp):
    1. public class TriggerInput
    2. {
    3.     string name;
    4.     bool used;
    5.  
    6.     public TriggerInput(string name)
    7.     {
    8.         this.name = name;
    9.     }
    10.  
    11.     public bool Pulled
    12.     {
    13.         get
    14.         {
    15.             if (! used && Input.GetAxis(name) > 0)
    16.             {
    17.                 used = true;
    18.                 return true;
    19.             }
    20.             else if(Input.GetAxis(name) == 0)
    21.             {
    22.                 used = false;
    23.             }
    24.             return false;
    25.         }
    26.     }
    27. }

    you can then use the triggerInput class this way.
    Code (CSharp):
    1.     public bool Fire2
    2.     {
    3.         get
    4.         {
    5.           return RightTrigger.Pulled;
    6.         }
    7.     }
     
  6. jedimndtrick

    jedimndtrick

    Joined:
    Oct 13, 2023
    Posts:
    1
    I modified the above code slightly. I used the 9th and 10th axes in the Input manager and named these "LTrigger" and "RTrigger" respectively. I then created a script to register the trigger inputs as a button press. I needed this modification so the player couldn't hold down the trigger button and have an action repeat. Here's the code I came up with in a class called InputUtility:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public static class InputUtility
    4. {
    5.     private const string leftTriggerName = "LTrigger";
    6.     private const string rightTriggerName = "RTrigger";
    7.     private static bool used;
    8.  
    9.     public static bool LTriggerPulled
    10.     {
    11.         get
    12.         {
    13.             if (!used && Input.GetAxis(leftTriggerName) > 0)
    14.             {
    15.                 used = true;
    16.                 return true;
    17.             }
    18.             else if (used && Input.GetAxis(leftTriggerName) == 0)
    19.             {
    20.                 used = false;
    21.             }
    22.             return false;
    23.         }
    24.     }
    25.  
    26.     public static bool RTriggerPulled
    27.     {
    28.         get
    29.         {
    30.             if (!used && Input.GetAxis(rightTriggerName) > 0)
    31.             {
    32.                 used = true;
    33.                 return true;
    34.             }
    35.             else if (used && Input.GetAxis(rightTriggerName) == 0)
    36.             {
    37.                 used = false;
    38.             }
    39.             return false;
    40.         }
    41.     }
    42. }
    And to call the code in another class for the Left Trigger input, you'd write it as such:
    Code (CSharp):
    1. if (InputUtility.LTriggerPulled)
    2.     Do.Something;
    If there's a better method of implementation, I've love to know!