Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
2
Question by Davste · Feb 18, 2012 at 08:46 AM · c#detectiongamepad

How can I detect if a gamepad is present?

I need to detect if currently connected. From there, I will remove the second player if it isn't.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
7

Answer by rbisso · Oct 15, 2012 at 10:01 PM

On PC, you can use Input.GetJoystickNames(), which returns an array of strings of the names of the joysticks that are connected. If that array is empty, there are no pnp joysticks connected, I guess.

I hope that helps!

I'm currently trying to figure out how to tell if there is a Bluetooth joystick connected on an Android device, but that information doesn't seem to be quite as handily available.

Comment
Add comment · Show 2 · 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 GeekyTime · Sep 14, 2014 at 05:39 AM 1
Share

I'd guess that if your gamepad isn't showing up in Input.GetJoystickNames(), it is registering itself as a bluetooth keyboard to try to get better compatibility with older versions of Android.

For whatever that's worth 2 years later...

avatar image Braun0 · May 11, 2015 at 05:43 PM 1
Share

rbisso, did you ever find a solution to your issue: "I'm currently trying to figure out how to tell if there is a Bluetooth joystick connected on an Android device"?

I am seeking an answer to the same question.

avatar image
2

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

you can do this in C#

 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 4 · 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 mvlsilva · Sep 13, 2017 at 01:14 AM 0
Share

Amazing soluction! But if i have two PS4 controllers conected in same time, or two Xbox One controllers? how could it differentiate?

avatar image Stanlyhalo · Nov 21, 2018 at 02:48 AM 0
Share

Whats the names[x].Length == A $$anonymous$$eyboard?

avatar image ScorchYT Stanlyhalo · Aug 06, 2021 at 07:44 AM 0
Share

It seems to have done something to your letter K, but I'm guessing you're asking what the length will be is there is a keyboard. It will be 0 for keyboard.

(You probably already know this because I'm 2 years late.)

avatar image rage_co ScorchYT · Aug 06, 2021 at 08:54 AM 0
Share

This is actually a problem with both answers and forums, capital M and K and te of some posts which are like 2 or more year olds have been replaced by $$anonymous$$.....i guess it could be the anonymous, but that'd be a pretty dumb and pointless thing to do for some hackers

avatar image
1

Answer by Lloyd_RedironLabs · Jan 18, 2016 at 06:32 PM

FYI, on PC I'm finding some GamePads (MOGA for example) stay registered as connected in Windows 10 --- even months later. And generate an Axis value if -1,-1.

Not sure if that helps anyone else. But I'll probably create an exception where until a joysticks value changes for the first time it continues to consider them disabled.

Comment
Add comment · 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

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Adapt OnMouseDown to control with Gamepad? 0 Answers

chronological order of collision to give a winner unity 2d 4.3 1 Answer

Raycast causes all enemies to attack 1 Answer


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