Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Tanoshimi2000 · Nov 16, 2016 at 12:43 PM · inputjoystickmultiplecontrollers

How can i tell which joystick input came from?

Old question, still not answered sufficiently.

I have a gaming station that has a steering wheel controller, flight stick (hotas), light gun, and xbox 360 controller connected to it. I have written games for each of those, and a menu to select which game to play. But I need to ensure the input for the right controller is used with the right game, and no other input is accepted. For example, you can't use the steering wheel or light gun to fly the helicopter, and you can't use the hotas or light gun to steer the car.

I was going to make a separate input for each controller, but the problem there is, I don't know which controller will be which number each time. Basically, I can't set up an Axis called "Steering Wheel Accelerator" = Joystick3.Axis1, because the next time I play the game, the Steering Wheel could be Joystick0 or Joystick1.

So, simple question, how can I tell which controller the input came from? Or, how can I ignore input from all but one controller? Or, how can I be sure that a specific controller is always JoystickN?

(Hint: this would all be so simple if, instead of Joystick# we could feed GetAxis(JoystickName). Seems like a simple fix.)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by darbotron · Nov 16, 2016 at 08:55 PM

As always in software engineering the answer is a layer of indirection...

This code should work for buttons, it's more or less what I do in my library code to fix the issue you're experiencing (I flung this code together for this answer, so apologies if it doesn't work - the idea does ;) )

     public class JoystickButton
     {
         public string        _strJoystickName;
         public int            _iButtonNumber;            // 0 indexed
         public int            _iJoystickNumber;        // 1 indexed
         public string        _strButtonIdentifier;    
 
         public void InitialiseButtonIdentifier( string strJoystickName, int iButtonNumber )
         {
             _strJoystickName    = strJoystickName;
             _iButtonNumber        = iButtonNumber;
 
             string[] astrJoysticks = Input.GetJoystickNames();
 
             // find the named joystick in the array
             // array position corresponds to joystick number
             for( int i = 0; i < astrJoysticks.Length; ++ i )
             {
                 if( _strJoystickName == astrJoysticks[ i ] )
                 {
                     _iJoystickNumber = ( i + 1 );
                     _strButtonIdentifier = string.Format(    "joystick{0}button{1}", 
                                                             _iJoystickNumber, 
                                                             _iButtonNumber );
                 }
             }
         }
 
         public bool ButtonIsPressed()
         {
             // N.B. you could use Input.KeyDown( KeyCode ) if you parse the string 
             // into a value of the KeyCode enum http://stackoverflow.com/a/16104
             return Input.GetButtonDown( _strButtonIdentifier );
         }
     }

You can extend it to work with axes too, but you need to manually add one to the Input manager for every axis you want to use on every joystick and use a naming convention for them so you can use string.Format() to generate the string for the axis programmatically.

So, the only thing to say now is that you'd have to re-generate any instances of JoystickButton you have if the array returned by Input.GetJoystickNames() changes.

Hope that helps!

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tanoshimi2000 · Nov 17, 2016 at 12:06 PM 0
Share

Yes, I'd considered this approach, and it may well be the only one. If a better answer doesn't come soon, I'll mark your answer correct.

$$anonymous$$y problem with this approach is that I still have to create all the items in the Input $$anonymous$$anager. I have 5 controllers (Flight Stick, Steering Wheel, Light Gun, X360 Gamepad, and another Gamepad), and I want to make sure the input for correct controller is used. Can't fly the helo sim with the steering wheel. To get this approach to work, I'd have to make all of the axis and all of the buttons for joystick 1 through joystick 5. Wanting to avoid that, especially because I have to do that for each project I use.

All this because we can't get an Input.GexAxis(JoystickName, AxisName) function!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Joystick Input Trigger all Inputs 4 Answers

Contollers are unusable when already plugged in when starting build/editor 0 Answers

Twin stick control 2D 1 Answer

Moving Unity DualTouchControls/Joystick on touch but also functioning on initial touch. 0 Answers

Im trying to make a top-down couch co-op game for the Xbox one and Im having issues getting player one and player two game pad controllers to control two seperate players 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges