Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Joystick Button: Fires on Both Controllers

Discussion in 'Scripting' started by renman3000, Mar 13, 2018.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Hi there
    I am mapping Xbox like controllers to a local 2 player multi, game. I have movement along the joystick controllers working fine, so each player recognizes the movement via his own joystick.

    My issue is that, its a different story for buttons upon the joysticks. Despite mapping an input value as "P1_Punch" and "P2_Punch", in the Input Manager, and defining those to each player (P1 and P2, respectively), when I define each's positive button as "joystick button 0", when I press that button on Joystick 1, it fires both players. if I press that button on P2 joystick, nothing happens.

    I have double checked, each Input is set to different joysticks and the players recognize themselves as requiring "P1_Punch" and "P2_Punch" respectively.


    Any ideas?

     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Honestly my best advice is to not use the input system for controllers, move to InControl for basic stuff and Rewired for more advanced control.
     
    renman3000 likes this.
  3. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681

    Yes, I bought InControl yesterday, but was just thinking I would give Unity a go.

    Is it really that much better? How could Unity be so bad? Is this why there is an die harder Unreal, phenomenon?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    A very similar question came up last week about the Joycon controller. So apparently it's not specific to that.

    Have you tried changing the Axis on these?
     
    renman3000 likes this.
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    You don't have to use InControl or Rewired. Unity's stuff works — but we used a somewhat different approach to detecting buttons. We actually use Input.GetKey, which works not only for actual keys, but also for mouse and joystick buttons.

    With just a little bit of effort you can even set up a fully runtime-configurable input system that supports multiple (and hot-swappable!) joysticks. We did this for a recent demo and it worked just fine. (In fact we had started with Rewired, but got frustrated with it, and eventually ripped it out and put in our own much simpler system.)

    Read all about it, and download a demo package that includes all the code, here: Configurable Input in Unity.
     
    ivaylo5ev, mnkyby78 and renman3000 like this.
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Love your blogs!
     
    JoeStrout likes this.
  7. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    The problem is that InControl is extremely easy to manage your controllers correctly and easily - so easy that there's zero reason to use the built in Input system for controllers until they ship the new version.

    Basically you can fight and struggle or just get something that instantly works and makes sense for chump change.
     
    renman3000 likes this.
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681

    So I just changed the Axis, on P2_Punch, as they (P1_Punch and P2_Punch) were both on Axis: 4th Axis (Joysticks), however, now that they are on 4th and 5th respectively, the same issue occurs.

    I also changed the Type from Key or Mouse Button, to Joystick Axis, but the same results. Under my current system Buttons are applied to both players from the same controller.

    Hmmm.
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    I think this matches the naming in KeyCode, so the "Positive Button" should be set to "joystick 1 button 0". Not sure, though.

    We do what @JoeStrout is saying, and use Input.GetKey for everything. So there's a file in PersistentDataPath that contains mappings from virtual buttons ("attack") to real buttons ("Joystick 1 button 3"). That makes it pretty easy to make the backend to whatever rebind menu you'd like, and it's also easy for everyone to edit the keybinds during development.

    I think the GetButton system is pretty horrid; the interface is clunky and it's easy to mess up. The new input system will ship when it ships, so that's not an option. Rolling your own based on GetKey is probably the best bet.
     
    website_unity and JoeStrout like this.
  10. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    Hey yeah, I think @Baste has the critical insight there. You have to name your buttons as he said.

    But I also agree with him in his agreement with me about just using GetKey. :) The only thing the Input Manager is actually needed for is reading joystick or mouse axes.
     
  11. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    @JoeStrout @Baste

    Hi guys, thanks, so I had been using GetButton with the results originally mentioned (one controller firing on both players (despite separate commands/defintions etc), and I switched to GetKey and defined the type as Mouse or Key Button.

    But now I get the error that the Input Key, is unknown.


    Thanks for your help. I am going to be bouncing between using Unity and InControl until I get one working perfectly. Thanks
     
  12. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    When using Input.GetKey, you use the strings here (bottom of page), not the ones defined in the Input Manager. That's why we recommended ditching Input.GetButton, because you don't want to have to fiddle with the Input Manager.

    It's better to use the KeyCode overload:
    Code (csharp):
    1. Input.GetKey(KeyCode.Joystick1Button3);
     
    mnkyby78 and JoeStrout like this.
  13. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681

    Hi still calls both.
    Code (csharp):
    1.  
    2.             bool value = Input.GetKey(KeyCode.JoystickButton0);
    3.             if (value == true) {
    4.                 print ("PUNCH : " + transform.name);
    5.             }
    6.  
     
  14. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    That's because you didn't copy my code! JoystickButton0 means "Button 0 on all joysticks". Joystick1Button0 means "Button 0 on joystick 1"
     
    renman3000 likes this.
  15. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Ooo!
     
  16. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Hi, Thanks.
    What are you entering for your "Positive Button" field? When I enter "Button 0 on joystick 1", the field info disappears and defaults to nothing, when I leave the field box.
     
  17. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    joystick 1 button 0, works...

    but with this

    Code (csharp):
    1.  
    2. bool value = Input.GetKey(KeyCode.Joystick1Button0);
    3.  
    a) there is no response and b ) still does not differentiate between two controllers.

    what a nightmare.
     
  18. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,842
    Nothing! Please read carefully. We are suggesting you use Input.GetKey instead of using the Input Manager. With this technique, you do not define any Input Manager input for these keys. Checking for a joystick button is exactly like checking for the "Z" key, or the spacebar, or anything else, with GetKey.

    The input manager is not involved.

    The way to fix your Input Manager entry is to click the little gear icon, and choose "Delete" (or perhaps it's "Remove" — I forget which) from the contextual menu.

    I really don't know how to be any clearer about this.

    (b) yes it does, and how would you know, given (a)? ;) I think it is differentiating between your controllers, but chances are, you've been swapping them around so much while working on all this that neither of them is Joystick 1 at the moment.

    Why don't you download and try the project on my blog? That has auto-detection code, so you'll quickly be able to see what your controller buttons are identified as.
     
  19. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681


    Ok!
    Will do!
     
    JoeStrout likes this.
  20. aloften

    aloften

    Joined:
    Aug 25, 2017
    Posts:
    18
    I KNOW this is old but I just wanted to help...I stumbled upon this forum and was very lost at where to go.

    If anyone else here wants to use the input manager(and the "Input.GetButton('P1ButtonName')" and has the joystick set to the proper number you ALSO have to set the joystick number in the button assignment.

    upload_2020-9-4_19-15-58.png

    In other words....Wteff is the "Joy Num" for?? lol I bet it actually does work in some scenarios...but either way.

    Anyways...this seems to work very well.

    Everywhere I looked online set to the "Positive Button" to "joystick button 0" but that seems to pick up all joystick button 0 calls. Instead, use "joystick 1 button 0" and update the joystick number on each joystick action. Its not like im against something like rewired or incontrol I just think....use those out of convenience, not substitution of knowledge.