Back to Top

Home ยป Tutorials

Unity Game Programming Tutorials

Using the keyboard to set player's position

Welcome to a new Unity C# tutorial! This time we will make use of the Update() method, writing some code that will change player's position in real time whenever we press one of the W, S, A or D keys. I have chosen those keys because they are often used to control player's movement in shooter games, but you can pick any other key.

Before we begin, here's a little tip that may prove to be useful, now or in the future. As you may know, I have a full-time job as a programmer, so I've got several versions of Visual Studio installed on my computer, because I need to provide support for older software as well.

Sometimes Unity will misbehave after a Visual Studio update, choosing an old VS version. To fix this issue, choose Edit from within the Unity editor's menu, and then click Preferences.


preferences

Click the dropdown menu that's located at the right side of the External Script Editor field, and then pick the desired Visual Studio version.


vs version

I have chosen the newest "Visual Studio 2019 (Community)" edition, because that's what I use for my Unity projects, but feel free to use any other version of the IDE.

Let's take a good look at the entire source code that's used for our project.


player movement

Actually, the code has a lot in common with our previous PlayerReset script. First of all, it uses the same namespaces and a class which inherits from MonoBehavior. Then, it uses an identical Start() method to reset player's position, moving our cube in the center of the screen regardless of its initial position.

The new stuff happens inside the Update() method. The code begins with two comment lines:

// we can use two forward slashes to write comments that explain our code

// press the W, S, A or D keys to set a new position for the player

Yes, we can use two forward slash characters to add human-readable explanations to our code, which will be ignored when the code is run. This allows us to write insightful comments, which will come in very handy after a few months or years, when we will need to revisit the source code of an old project.

Programmers also use comments to disable sections of their code temporarily or permanently. To comment a larger block, start the comment with /* and end it with */

It's time to dissect the code inside the Update() method! It may look a bit scary at first, but it consists of four very similar blocks, which control the position of our player on the positive and negative X and Y axis. Let's take a look at the first code block.

if (Input.GetKeyDown(KeyCode.W))

{

    transform.position = new Vector3(0, 1, 0);

}

The "if" keyword executes the commands which are placed between the pair of brackets if a certain condition is true. Its general form looks like this:

if (some condition is true)

{

    execute the code that's been placed here

}

Going back to our Unity code, we discover that the condition that needs to be evaluated is (Input.GetKeyDown(KeyCode.W)). In other words, we need to check if the Input class, which is Unity's interface into the input system, has detected that the "W" key is being held down.

If that happens, transform.position (the X, Y and Z coordinates of our player) gets the values which are set by a vector that sets the Y component to 1, moving our cube (player) above the origin on the Y axis.

The process repeats for the "S", "A" and "D" keys. And if you want to change the keys, you can make use of Visual Studio's autocomplete feature to explore all the available options.

Let's pretend that I want to replace the "W" key with "Q". Delete ".W" (yes, including the dot) from the corresponding line of code, and then type a dot after KeyCode; VS will show you all the available options, including dozens of joystick buttons, the function keys, mouse buttons, and more.


key optionsThis wraps up our tutorial. I'll see you in the next one, when we will start creating a script that's a bit more complex. Don't worry, though, I will do my best to help you understand all the new concepts.