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
2
Question by Cheef · Oct 02, 2015 at 11:26 AM · joystickgamepadjoysticks

Detecting joystick disconnect

I see this question has been asked before, but I can't find a solution to it.

So I have a local multiplayer setup with two xb-one gamepads and that's functioning as expected. I'm keeping track of Input.GetJoystickNames().Length. If I disconnect a joystick during gameplay or even in editor, the .Length keeps it's value at 2.

I have my code setup so that if the value=1, make the 2nd player use the keyboard+mouse.

Now I have to restart the game/editor to set Input.GetJoystickNames().Length to match connected joysticks number.

Is there any method to detect a joystick disconnection??

Comment
Add comment · Show 1
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 Alakanu · Nov 19, 2015 at 09:52 AM 0
Share

I would love to know aswell, same issue here.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by petersvp · Aug 26, 2016 at 09:20 PM

Implement your own InputManager using Win32's RawInput API in C++. This way you can detect disconnections and handle them yourself. You can even differentiate between multiple mice this way.

Here is sample implementation but for mice. Alter is to suit your needs. It properly detects XBOX Gamepads.

C++ DLL: http://pastebin.com/0Szi8ga6 C# script: http://pastebin.com/4h3CqpYy

Note that in Editor, Unity steals RawInput devices very feequently. I did not had any issues in a build, however.

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 Alakanu · Aug 27, 2016 at 06:40 AM 0
Share

Nice job there, thanks.

avatar image
1

Answer by robpuk38 · Nov 28, 2016 at 02:06 PM

 private int Xbox_One_Controller = 0;
      private int PS4_Controller = 0;
  void Update()
  {
  string[] names = Input.GetJoystickNames();
          for (int x = 0; x < names.Length; x++)
          {
              print(names[x].Length);
              if (names[x].Length == 19)
              {
                  print("PS4 CONTROLLER IS CONNECTED");
                  PS4_Controller = 1;
                  Xbox_One_Controller = 0;
              }
              if (names[x].Length == 33)
              {
                  print("XBOX ONE CONTROLLER IS CONNECTED");
                  //set a controller bool to true
                  PS4_Controller = 0;
                  Xbox_One_Controller = 1;
  
              }
          }
  
  
  if(Xbox_One_Controller == 1)
  {
  //do something
  }
  else if(PS4_Controller == 1)
  {
  //do something
  }
  else
  {
  // there is no controllers
  }
  }
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 RodrigoSeVeN · Mar 17, 2018 at 08:30 PM 0
Share

This answer could be expanded to include any controller, save that information in a list and compare the size for when Input.GetJoystickNames() changes its Length. When that happens, you'd just need to compare the new list to the saved one to figure out which controller is not on the list anymore.

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

35 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

Related Questions

Weird things happening with gamepad input 0 Answers

Gamepad recognized as joystick and doesn't work 0 Answers

Virtual joysticks for mobile possible? (Mimic controller left & right analogs) 0 Answers

nested two joysticks 0 Answers

X360 Controller detection gives uneven quadrants 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