Unity Community

Results 1 to 8 of 8
  1. XBOX 360 Controller Trigger Button

    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:

    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. Location
    Zürich, Switzerland
    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. Location
    You'll have to compare the current input state to the one in the previous frame, which you will have to record:

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

  4. Posts
    Can anyone help convert the above code into C? Really having trouble with this.

  5. Posts
    Quote Originally Posted by Newnab View Post
    Can anyone help convert the above code into C? Really having trouble with this.
    C as in 'C'? Why do you need the code in C?

  6. Posts
    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 by Newnab; 10-31-2010 at 04:36 PM.

  7. Posts
    Quote Originally Posted by Newnab View Post
    As in C#.
    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):

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

  8. Posts
    Only just got around to implementing this, it works, thanks!

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts