Let’s get physical, with Inputs

addam davis
4 min readMay 8, 2021

--

You can make cubes and capsules and assign them automatic movement. Wouldn’t it be great if you could move these objects as you please. If you pressed the ‘W’ key and the object went up. Well, my friends, I have an exciting lesson for you. Today we are going to assign inputs to your player!

Unity has a laundry list of available inputs in its registry, 18 in total. On the top of the screen you should click onto Edit, scroll down and select project setting. This will open a window and on the left, you will see Input. Selecting this will show you all the inputs you have available to you and they are already assigned to keys, we just need to connect these to your player.

If you select the horizontal axis, you will see that it has a negative/positive button. These are defaulted to the left and right arrows. There is also an alternate negative and positive assigned to the ‘A’ and ‘D’ keys.

We now need to figure out what is being returned because the axis doesn’t tell you. We also need to store these inputs as variables, and buttons are on/off making them bools. These inputs being bools are defaulted in their returns, meaning when pressed they return a 1 and when not pressed, they return a 0.

Open the player script and you will inter the new variables under the void update method.

Now let’s break this down. The input is the top of the hierarchy so we must begin with input. We need to select a specific axis, and the horizontal is a string and strings must always be in quotes.

Now we need to insert this variable into our Translate line.

We declared the horizontal input using a local variable type float and we set it to input.GetAxis(“Horizontal”);

We can break it down ever further with some exciting math. We start with the Vector3, which we now represent the x, y, and z axis. (0, 0, 0) and it is currently set to (1, 0, 0). We are multiplying that by the horizontal speed which when the button is pressed it is 1. If the button isn’t pressed it is a 0.

So, if the button isn’t pressed the equation is, (1, 0, 0) * 0 = 0. Then in our code we are multiplying by _speed, which is set to 3.5f. 0 * 3.5f = 0. Finally, we are multiplying that by the Time.deltaTime, which is real time. 0 * real time = 0.

However, if the button is pressed and the horizontal input becomes one the math changes. (1, 0, 0) * 1 = (1, 0, 0) * 3.5f = (3.5f, 0, 0) * real time = (3.5 meters/second, 0, 0) and this is all on the x axis. I know what you’re thinking, and yes, math is super fun.

Now when we use out input the player will move on the horizontal axis at 3.5 meters/second. An alternate way to write this would have been:

Now you can use the exact same method to unlock your vertical inputs. You must make it into a variable at the top of the void update method. Of course, you would need to use the GetAxis(“Vertical”) and instead of using vector3.right, you would use vecor3.up.

The math is the same and now that you understand it, you can easily manipulate it. Instead of having the two lines of code, one for horizontal and the other for vertical you could have written:

Welcome to inputs, now when you start the game in the edit view you will be able to use WASD to control your player and have him run across the screen at whatever speeds you set.

--

--