Use The WASD, Luke!

Christian Fredericksen
4 min readMar 2, 2022

--

Objective: Assign player movement to these keys.

Now that we have our hyperspace speed issue under control, let’s figure out how to control our player’s up, down, left, and right movement.

Up and Down. Left and Right. More specifically, to put a very sharp point on it, the X and Y axes. So, check this out. In Unity go to Edit > Project Settings… and it should open something like this:

This is where you can configure the controls to use with the Unity engine.

And should already be on the Input (<remember that) Manager. If not, select that and we’ll be together again.

Clicking on Axes will open up a list of 18 different controls.

The first 6 are preset defaults and are the ones we are interested in at the moment. Particularly the Horizontal control. Click that open.

We find some useful information here. Like a name and some navigation controls. It shows us Negative and Positive Buttons are the left and right keys and Alt Negative and Positive Buttons are the a and d keys. These will be our player inputs to control the X axis. But how do we get this data into our code? As we learn how to do this, see the pattern of the first three bold words in this article and how they connect with our code. If you are new to coding, this is an example of how to THINK like a coder and connect the pieces together.

Close down the Project Settings view and go back to your code. Inside the Update function declare a new variable with assigned value. That value is going to be these left, right, a, and d keys. And it looks like this:

So, we have a float variable to accept the float value of _speed. (We’ll get to that next) and it’s name is horizontalInput. And when we call on that variable it means this:

  1. horizontalInput
  2. says
  3. “Hey, Input…
  4. …get me an axis…
  5. …this one.

We now have a variable that controls our movement along the X axis (left and right). Let’s add this to our line of code like this:

And there’s the reference to _speed that our horizontalInput variable needs to understand.

CTRL+S(ave) your work and test this out in Unity. The left and right arrows as well as the a and d keys now operational.

Now, if you’re wondering about the details of how this works let’s look at the code like this. First there’s this bit:

Negative and Positive meaning -1 and 1 respectively. When we use one of these inputs the math of this code:

Breaks down to three possibilities:

  1. < or a to move right
  2. no input at all
  3. > or d to move left

You got it, my friend! X axis control. As a challenge, with the knowledge we’ve gained here, add the Y axis control. Or you could cheat and wait to see it in the next article.

Some of you more experienced readers following along will definitely see cleaner and more optimal/efficient ways of writing this code. And you’re exactly right. Like in my example ^there.

Each one of those could have been called realTime. And as we progress through these articles we will discover how to write more eloquent code. There is no right or wrong way to write code if it works. There are just more efficient ways of getting it to work. And that will come in time.

In our next article we will discuss setting boundaries for our player.

Until then, code your imagination to life.

--

--