Day 6: Basic Input in Unity

Blake Zoeckler
3 min readApr 25, 2021

All games require input of some sort. If the player can’t control what’s happening on the screen, then it’s not a game at all, but a movie (or some horribly drawn-out cut scene). Games are played partly for the interactivity, and the ability to be in charge of the action.

With that said, it’s time to follow up on the previous section about player movement, and start letting the player actually control the game.

The first thing we need to do is check the Input Manager in Unity.

This shows the various control axes available to the input system, and what keyboard or mouse buttons affect those controls. Here we can see that the A and D keys affect the Horizontal axis, while W and S affect the Vertical axis. These axes can also be affected by the arrow keys.

Now lets put this to use in our code.

The main thing to note is Input.GetAxis(“Horizontal”); This tells Unity input to get the current value provided by the Horizontal Axis, which is controlled by A and D keys. This returns a float number between -1 and 1. We get 1 when D is held, and -1 when A is held. By multiplying this factor to our speed, we can control whether the square moves left or right by using the keyboard!

When I press ‘D’, the square moves right. When I press ‘A’, the square moves left.

We can do the a similar thing to add in vertical movement as well.

Now we are getting the “Vertical” axis as well. We use that to control how much the square moves up and down by multiplying it to the “Vector3.up” vector.

Now we can move this square around by using all of the WASD keys, the arrow keys, or even a controller joystick!

Finally, there’s a bunch of changeable settings in the Input Manager that we can use to change the behavior of the movement.

Here I’ve reduced the Gravity and Sensitivity, so the square will accelerate much more slowly, as if it were on ice.

This has all been created by using Unity’s “old” Input system. If you want to learn more about the new Unity input system that has many more features, Alex Somerville created a nice article about that here: https://mrlovelies.medium.com/unity-dev-player-movement-new-input-system-6291f257a77d

--

--

Blake Zoeckler

I’m a passionate and talented software engineer seeking an opportunity in game development.