How do you set up controller input in Unity?

Andrew Alteri
3 min readJun 6, 2021
Switch, Dualshock, and Xbox Controllers

Mouse and keyboard vs. controller is one of the longest-running debates in the video game community. No matter which side you’re on, there is no denying that games are most accessible when they support both inputs. While challenging myself with bonus features for Unit 2 of my Create with Code project, I decided to enable play through a gamepad controller. Although I was able to set up joystick compatibility with ease, I struggled to get my Unity project to recognize the buttons on an Xbox controller. After digging into resources and documentation, I figured out the issue and was able to make my game more accessible. Read on to learn more.

*(I used an Xbox controller since Windows has plug-and-play support for Xbox, but Unity supports a wide range of controller devices.)

The Unit 2 Create with Code challenge was to create a game where a player controlled a farmer that had to feed the oncoming animals. The animals spawned at the top of the screen and rushed at the player. If the player successfully fired a food projectile at the animal, then the animal would disappear. If the animal ran past the player, the game ended. For my game, I chose giant farm animals that needed to be fed by a sandwich throwing farmer.

Isometric game where farmer is feeding oncoming giant animals
Honey, I Blew Up the Unity Farm

Before coding the player inputs, I needed to modify my Input Manager in the Unity editor. To do this, I clicked Edit > Project Settings > Input Manager inside the editor. To get my character to throw sandwiches at the farm animals, I updated the “Fire1” settings.

Here are my settings for firing on spacebar:

Here are my settings for firing on ‘A’ button or right shoulder button:

*(Here is a link that further explains initial controller setup in Unity editor.)

Now for the real magic of Unity: C# integration! In my PlayerController script that controls player movement and actions, I declared a public GameObject variable at the top of the script called projectilePrefab. This enables me to add any object into the projecticlePrefab variable in the Unity editor so the player can throw food to the animals.

Here is my if statement that triggers the player to throw food in response to a button press:

According to this code block, if a certain button is pressed, then a projectile is instantiated, and the string “Fire” is logged in the console. I set up both keyboard and controller inputs.

*(Here is Unity’s documentation on the Instantiate method)

During the process, I encountered a roadblock that caused my projectile, a sandwich, to glitch out. GetButtonDown returns true during the frame that the user presses the button. This allows one projectile per button press. Originally, I thought I was supposed to use GetButton that continuously returns true while the button is held down. That caused my projectile to instantiate repeatedly, never reaching the belly of a hungry farm animal. With a simple change to Input.GetButtonDown, I was able to instantiate a sandwich on a button press and feed the animals.

Hopefully, this post will help you make your Unity game more accessible by adding controller support. If you would like to check out my entire repo on Create with Code Unit 2, check out my GitHub. I encourage feedback on how I can improve my C# code. Have a great week and keep creating!

--

--