O'Reilly logo

Unity Game Development Scripting by Kyle D'Aoust

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating the device detector

The first function we'll create is one that will detect whether we actually have a gamepad plugged in. Unity inherently gives us a way to make this very easy.

Adding the variables needed

First, we'll add variables that we'll use in the detection and identification functions. Add these to the top of your script, just after the class declaration:

bool isControllerConnected = false;
public string Controller = "";

This Boolean will be used in later functions to determine whether there is a gamepad connected. The string will be used to hold the name of the gamepad connected.

Creating the detection function

Our next step is to add the DetectController function. This will use the Boolean we created earlier and check whether there ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required