XBOX 360 Controller Trigger Button

Discussion in 'Editor & General Support' started by Mackey@ehsiplus.com, Jun 29, 2009.

  1. Mackey@ehsiplus.com

    Mackey@ehsiplus.com

    New Member

    Joined:
    Jun 27, 2009
    Messages:
    45
    I am trying to use a xbox controller for my fps.
    I use the XBOX 360 Driver for Mac by Colin Monro

    When i use the Joystick Test Program at:
    http://www.rotorheadgame.com/unity/joytest.html

    It says the Left/Right Triggers are 5th/6th Axis
    NOT a 'joystick button x' (x being actual button number like 0 for fire)

    I used the Input Manager to make another 'Fire1' Axis
    Type: 'Joystick Axis'
    Axis: 'Joystick 6th Axis'

    Now first off since there is nothing i can put for button positive (because it only allows text like 'joystick button 0'... cant enter 'Joystick 6th Axis' the GUI wont accept this) i am force to change my code from Input.GetButtonDown("Fire1") to Input.GetAxis("Fire1") > 0.0 to tell if the use press the fire button.... The problem is that GetButtonDown only fire true in the FRAME that button was pressed in which great for a single fire thing (that is what i want) but for me to be able to use the trigger from the xbox 360 controller to fire a weapon (single shot for now) and since the 'Button Positive' wont take the value 'Joystick 6th Axis' i have to use GetAxis("Fire")
    now this fires in every frame...

    How can i only do my weapon fire code (Which instantiates a missle game object) for the frame the joystick button was pressed in (basically get the same functionally of the GetButtonDown but with a GetAxis so my weapon only fires once per trigger pull .. not fire off 100+ missiles becuase i have the trigger held down and GetAxis("Fire1") > 0.0 will ALWAYS BE TRUE.. Even on the way down of pulling the trigger and back up... WHEN the trigger is fully neutral is the ONLY time GetAxis("Fire1") will be less than or equal to 0)

    So i need help in 'tweaking' the normal code used in the tutorials use GetAxis instead of GetButtonDown but all the features of GetButtonDown to ensure 1 shot per trigger pull or how do i make the Input Manager treat the trigger pull like a regular button fire...

    Or any other info on how to use the XBOX 360 Right trigger to fire a weapon like the keyboard 'Ctrl' Key
     
  2. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,590
    The two axis only work with XInput.
    With DirectInput and libraries like FreeJoy they are only recognized as buttons thus you get true from the first to the last moment they are pressed.

    Thats microsofts error because they just wanted to enforce XInput against any intelligence (and against a few hundred games that don't work right with them due to this not understandable decision)
     
  3. Daniel Brauer

    Daniel Brauer

    Member

    Joined:
    Aug 11, 2006
    Messages:
    3,291
    You'll have to compare the current input state to the one in the previous frame, which you will have to record:

    Code (csharp):
    1. private var oldTriggerHeld : boolean;
    2.  
    3. function Update() {
    4.     var newTriggerHeld = GetAxis("Fire1") > 0.0;
    5.     if (!oldTriggerHeld  newTriggerHeld)
    6.         Fire();
    7.     oldTriggerHeld = newTriggerHeld;
    8. }
     
  4. Newnab

    Newnab

    New Member

    Joined:
    Oct 25, 2010
    Messages:
    7
    Can anyone help convert the above code into C? Really having trouble with this.
     
  5. Jesse Anders

    Jesse Anders

    New Member

    Joined:
    Apr 5, 2008
    Messages:
    2,857
    C as in 'C'? Why do you need the code in C?
     
  6. Newnab

    Newnab

    New Member

    Joined:
    Oct 25, 2010
    Messages:
    7
    As in C#.

    Just because my current script for the controls is in C# and I'm trying to adapt it to work with a 360 controller and this exact same problem has been bothering me.

    I tried converting it to c# myself with what little knowledge I have of it and sort of failed. I got response from the triggers at least, but they fired the weapon once per frame once pressed.
     
    Last edited: Nov 1, 2010
  7. Jesse Anders

    Jesse Anders

    New Member

    Joined:
    Apr 5, 2008
    Messages:
    2,857
    Ah, just a typo then I guess. (C and C# are different languages, but I guess you probably know that.)

    Anyway, here's what that code might look like in C# (not compiled or tested):

    Code (csharp):
    1. public class MyClass : MonoBehaviour
    2. {
    3.     bool oldTriggerHeld;
    4.    
    5.     void Update()
    6.     {
    7.         bool newTriggerHeld = Input.GetAxis("Fire1") > 0f;
    8.         if (!oldTriggerHeld  newTriggerHeld) {
    9.             Fire();
    10.         }
    11.         oldTriggerHeld = newTriggerHeld;
    12.     }
    13. }
     
  8. Newnab

    Newnab

    New Member

    Joined:
    Oct 25, 2010
    Messages:
    7
    Only just got around to implementing this, it works, thanks!

    I was pretty close, just some minor differences but they made a big impact!