Adding User Input to Control a 2D Object

David Hunter Thornton
4 min readAug 8, 2022

--

How do I move an object with a specific button press?

Objective: Explain Unity’s User Input system and how that can be used in combination with a gamepad or keyboard to control the movement of an object.

This is exciting! We’re taking the first steps to turning this project into a game. We set up variables for movement in the last article; now its time to tell it to only move at the press of a button.

To do that, we need to access Unity’s Input manager. This is located under the “Edit” tab in the top left. Select “Project Settings” and in the new window choose “Input Manager”.

The “Axes” in this window is actually the plural of “Axis” not the tool used for chopping wood. In this list you can see various potential input types. Such as “Fire1”, “Jump”, or “Mouse X”. These are general descriptions that already have a number of button prompts assigned to them. Such as the Spacebar, or the “Cross” button on a Playstation Controller.

Additionally, if you desire, you can map specific keys or buttons to do things they might not otherwise be used for. If you wanted to map “Jump” to the “Circle” button as is standard practice in some JRPGs for example.

For now, we want to focus on the Horizontal and Vertical Inputs. We’re going to create a private variable for this, but instead of placing it at the very top, we’re going to put it inside the “Update” section of our code. We’re going to use the same logic for getting the “Y” coordinates from our “Transform” data like this article here.

So the logic would be “Input Manager” -> “Axes” -> “Vertical” for example. Also, since we’re placing the new variable in the Update section, then we don’t have to put “private” before it, because it can only be accessed locally anyway.

A few important notes:
1. We know this is a float variable, because the “Horizontal” and “Vertical” Inputs are going to return a decimal when input is collected from a key press or from a Joystick.
2. The name of this variable, “_verticalMovement”, is written as such, because it is common practice to use uppercase letters to separate words and make them easier to read. But the first letter should always be lowercase.
3. Input Manager and then “Axes” is called by using “Input.GetAxis”.
4. To tell input manager what we want to “get” or have returned, we need to surround it in parentheses. Also, since we’re inputting a “string” then it should always be in quotations.
5. As always, don’t forget the semi-colon at the end of the line.

Okay! One more thing to get this square moving correctly. We need to include the “_verticalMovement” variable inside our “math” equation at the bottom.

The only thing left is to check that everything works in Unity! By pressing “W”, “S”, “Up Arrow”, or “Down Arrow” our Player Character should move accordingly.

Now, if you repeat all of the same steps, but replace “vertical” with “horizontal” then you should have movement in both directions.
NOTE: DON’T COPY/PASTE!
You’re only hurting yourself if you copy-pasta your code. It takes practice to get good at this. Repetition really helps. And if you think through it each time your type it, then you’ll be better off in the long run.

It is worth mentioning that this isn’t the most optimal way to code this particular behavior. In fact, its a good rule of thumb, that if you’re repeating something exactly the same, then you could probably convert it into a variable.

If you only repeat something once, it isn’t a huge deal, but if we had to repeat that same line 3 or more times, then we should really clean up the code. Don’t stress about it right now if you’re still learning. Just know that this works, and there’s no “wrong” way to do one thing. There’s always more than one way to get the job done.

Friendly Reminder: Don’t forget to keep updating your project through GitHub/Git and saving any changes you make to a separate branch. You can check out my other articles to get a breakdown of those steps! Day 1–9

--

--