• 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
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

People who like this

0 Show 0
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

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
HER20081

People who like this

1 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.

My problem with this approach is that I still have to create all the items in the Input Manager. 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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

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

hello do any of you guys know how to make a 2 player Dpad game so me and my friend can play the game with a controller if so please tell me , i was having trouble and i was gonna ask if it is possible and if it is how do i do it 1 Answer

Weird things happening with gamepad input 0 Answers

I need to control my player using a joystick. I used the default joystick script in Unity, but i am not able to integrate it with the player. Please help. I am attaching the player script, its currently using keyboard input 0 Answers

Joystick 3rd Axis problem on Linux 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