[Need Help] Xbox 360/PC controller Triggers

Discussion in 'Scripting' started by apiotuch, Feb 4, 2010.

  1. apiotuch

    apiotuch

    Member

    Joined:
    Oct 8, 2009
    Messages:
    21
    I'm using an xbox/pc controller to Aim with one button, and then shoot with another button, but only when the aim button is being held down. The problem I have is when mapping the controls to the triggers. See the Left and Right triggers are recognized in unity as one input, specifically an axis, not a button. If you press down the left trigger, unity recognizes the axis to be > 0. If you press down the right trigger then unity recognizes the axis to be < 0. Here's my problem,

    the axis can't be > 0 AND < 0 at the same time :eek: .

    But i need to hold down both triggers to aim + shoot (Resident Evil Style). Is there a way to separate the two triggers? Or is there another solution?
  2. sybixsus2

    sybixsus2

    New Member

    Joined:
    Feb 16, 2009
    Messages:
    944
    No, there's no way to separate the triggers. I reported it as a bug some months back, but it's still open. I included a sample project with the report, but if you feel you have anything to add, I guess you could submit your own bug report.
  3. AmazingInt

    AmazingInt

    New Member

    Joined:
    Dec 7, 2009
    Messages:
    158
    It's not a bug: If you're using an original xbox pad on the pc (not a 360 one) then go into the windows control panel and using the xbox pad config manager assign the triggers to be both on different axis: You'll be able to happily use them both at the same time in unity then...

    Without setting them like this you'll struggle to find any software that will let you use them independantly.
  4. sybixsus2

    sybixsus2

    New Member

    Joined:
    Feb 16, 2009
    Messages:
    944
    That's funny. When I contacted Unity Support, they said it was, and asked me to submit a bug report.

    I'm talking about the 360 one. I assumed the original poster was too, since there was no officially PC compatible version of the original controller, and he does says XBox/PC.

    Well it's good to know that this is possible on the original controller, but there is no such option on the 360 controller, as it's intended to be read using XInput, not DirectInput. I don't know for sure but I suspect Unity is using DirectInput.
  5. AmazingInt

    AmazingInt

    New Member

    Joined:
    Dec 7, 2009
    Messages:
    158
    Ah my bad...I use an original xbox pad (all 16 buttons are potentiometers!) with xbmc it's shown as a windows gamepad.

    Damn new fangled 360 pads :(
  6. MatthewW

    MatthewW

    Member

    Joined:
    Nov 30, 2006
    Messages:
    1,260
    This is indeed a DirectInput versus XInput problem. You can't access the triggers independently through DirectInput (which appears to be deliberate on Microsoft's part, to push people into XInput).

    There is already a plugin for Unity that provides support: http://speps.fr/xinputdotnet . However, it isn't 100% "pure" CIL, as it claims, so you can't use it in the web player (and will likely need Unity Pro). Their DLLs aren't safe for distribution, either; you should recompile statically if you use them.
  7. apiotuch

    apiotuch

    Member

    Joined:
    Oct 8, 2009
    Messages:
    21
    I meant the 360 controller, not the original xbox.
  8. speps

    speps

    New Member

    Joined:
    Oct 13, 2009
    Messages:
    22
    Well, it's pure in the sense that it works on Unity/Mono and of course it's not 100% "pure" CIL because it uses DirectX. It's just an interface using P/Invoke and a "homemade" DLL.

    What do you mean by "not safe for distribution"?
  9. Dreamora

    Dreamora

    Member

    Joined:
    Apr 5, 2008
    Messages:
    26,586
    Actually thats incorrect.
    The Problem is a XBox360 DInput driver problem.
    Microsoft "fucked the triggers up" by intend.

    There are kind of three solutions:

    1. See above, the .net xinput thingy

    2. There are inofficial community supported drivers for the joypad that solves the problem.

    3. Work with the triggers as they are meant to by their design.
    The triggers are seen as a single joystick axis with the R Trigger going from 0 to 1 and the L trigger going from 0 to -1 on the same axis. If both are pressed at the same time they negate the effect of each other to offer a "smooth accelration + break" effect (as racing games and games with similar acc / break schemes are the primary reason for the triggers)
  10. MatthewW

    MatthewW

    Member

    Joined:
    Nov 30, 2006
    Messages:
    1,260
    The DLLs are dynamically linked and have DLL dependencies. It won't work on a fresh Windows 7 install, for install. In Visual Studio you can update the options to statically link--off the top of my head I think it's /MT instead of /MD on the code generation properties...
  11. speps

    speps

    New Member

    Joined:
    Oct 13, 2009
    Messages:
    22
    Thanks for the tip, I often forget to make the change in my projects. It's fixed now and available on my website (see signature).
  12. souzah

    souzah

    Member

    Joined:
    Jun 5, 2014
    Messages:
    1
    i'm new here, so first of all, hi to everyone!

    it happens that i also have a 360 pc wired controller, and im having the issue of the trigers axis being combined, could you show me a driver, or some other solution that would put them to work separately, im playing racing games so its very important to be able to accelerate or break, independently and smoothly.

    Thanks in advance ;)
  13. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Messages:
    11,057
    Do a search for InControl
  14. Maranasa

    Maranasa

    Member

    Joined:
    Mar 14, 2014
    Messages:
    3
    Its really not that hard and you don't need to download anything special. The trigger are set to axis 9 and axis 10. So you need to set your commands to them instead of axis 3. But where a lot of people go wrong is they script it like a button.

    You need to store the trigger input as a float and then use that as a condition for the action you want to preform. here is a short example script)

    (assume you have set fire1 to the right trigger [axis 10] and the left trigger to fire2 [axis 9])

    using UnityEngine;
    using System.Collections;

    public class Guns : MonoBehaviour
    {

    public float fireRate1;
    public float fireRate2;
    public Transform shotSpawn1;
    public Transform shotSpawn2;
    public GameObject shot1;
    public GameObject shot2;

    private float leftTrigger;
    private float rightTrigger;

    private float nextFire1;
    private float nextFire2;
    void FixedUpdate ()
    {
    leftTrigger = Input.GetAxis ("Fire2");
    rightTrigger = Input.GetAxis ("Fire1");
    if (rightTrigger > 0.1f && Time.time > nextFire1)
    {
    nextFire1 = Time.time + (fireRate1 * Time.deltaTime);
    Instantiate(shot1, shotSpawn1.position, shotSpawn1.rotation);
    }
    if (leftTrigger > 0.1f && Time.time > nextFire2)
    {
    nextFire2 = Time.time + (fireRate2 * Time.deltaTime);
    Instantiate(shot2, shotSpawn2.position, shotSpawn2.rotation);
    }
    }
    }