- Home /
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.
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.
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...
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.
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
}
}
Amazing soluction! But if i have two PS4 controllers conected in same time, or two Xbox One controllers? how could it differentiate?
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.)
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
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.
Your answer