Search Unity

Xbox Controller LT+RT at same time

Discussion in 'Scripting' started by Gaspedal, Jul 9, 2011.

  1. Gaspedal

    Gaspedal

    Joined:
    Mar 29, 2009
    Posts:
    376
    I use the xbox 360 controller for my game and I want set the LT for aim and RT for fire. Works fine when I not press LT or RT at same time. But both Buttons at same time doesn't work because LT and RT uses the same axis (3rd axis), LT (-1.0) and RT (1.0), when I press both buttons at same time I get a value of 0 (1 -1=0).

    How can I solve this problem ?
     
  2. Dreamblur

    Dreamblur

    Joined:
    Jun 18, 2011
    Posts:
    183
    Then just create a new virtual axis for one of them.
     
  3. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    LT + RT is by microsoft definition of the DirectPlay drivers for the X360 Pad a value of 0 actually. (Microsoft defines LT as 0 to -1 and RT as 0 to 1 on the same axis in DirectInput - Only in xinput they show up as distinct axis)

    so there is no problem to fix, its correct.
    its your code logic that requires fixing or you need to use the XInput .NEt assembly available for Unity to use the x360 controller that way
     
    Last edited: Jul 9, 2011
  4. Gaspedal

    Gaspedal

    Joined:
    Mar 29, 2009
    Posts:
    376
    Thanks,

    but try it self. I have also created a new virtual axis but it doesn't work.

    I can only select "3rd axis" for LT RT and when I press RT then the value of LT will be also changed (also reversed).

    My code:

    if (Input.GetAxis("Joy_Fire1") > 0) // set as 3rd axis
    aim = true;

    if (Input.GetAxis("Joy_Fire2") < 0) // set as 3rd axis
    fire = true;


    will now try it with XInput.Net
     
    Last edited: Jul 9, 2011
  5. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    Using a virtual axis won't do it. The driver that microsoft provides for the Pad on windows for DirectPlay (or for "not XInput") enforces them to be on the same axis, no matter what.

    The XInput extension is really the way to get there, its freely available, so no worries on that end
     
  6. Dreamblur

    Dreamblur

    Joined:
    Jun 18, 2011
    Posts:
    183
    If you're using 2 separate virtual buttons for them and you only need to know if a virtual button is being pressed, then use Input.GetButton instead of Input.GetAxis.

    EDIT: Actually, let me do some research regarding this since it's a tad interesting.
     
  7. Gaspedal

    Gaspedal

    Joined:
    Mar 29, 2009
    Posts:
    376
    Thanks, but Input.GetButton doesn't work with 3rd Axis (on LT RT). Result is always false. I have tested it today.
     
    Last edited: Jul 9, 2011
  8. Gaspedal

    Gaspedal

    Joined:
    Mar 29, 2009
    Posts:
    376
    Really interesting. With XInput it works !! I get 2 different values when I press LT RT at same time. I think this is a unity Input problem with virtual keys.

    Thanks dreamora for your great tip! :)
     
  9. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    No its no Unity bug, virtual key problem or anything alike.

    As mentioned above, thats a Microsoft thing. Microsoft decided that the DirectPlay drivers, used for anything but XInput, don't offer the buttons on seperated axis, instead they map to the same axis with LT from 0 to -1 and RT from 0 to +1.
    Unity can't do the slightest bit about it as unity only gets the final input value from the driver and thats already the sum of the buttons
     
  10. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    So basically just check:

    if < -0.1 then left button pressed
    if > 0.1 then right button pressed
    if abs(value)<0.1 then both buttons pressed
     
  11. Gaspedal

    Gaspedal

    Joined:
    Mar 29, 2009
    Posts:
    376
    forget it, it doesn't work, even with abs. I have tested all methods. Only with XInput works fine.

    And a value of <0.1 (=0) is nothing pressed or both buttons are pressed. ;)
     
    Last edited: Jul 10, 2011
    AdrianRio likes this.
  12. iamphantasm0

    iamphantasm0

    Joined:
    Jul 29, 2021
    Posts:
    1

    pls tell me how you did it I've been trying for a while now