Create a Player and Add Basic Input Movements in Unity

Andrea Zilio
4 min readApr 15, 2021

--

When creating a game, the temptation is big to immediately go for the visuals. In my first projects I fell for this trap too and believe me it is easy to spend hours on finding and creating assets. There is a better way. Before we should even think about assets, we need to understand and create the basic mechanics of our game. This is best done with placeholders.

Create your Player

Unity provides a number of basic shapes we can use as placeholders. If you right click in the hierarchy window you will see different objects you can add to your scene. A cube will work just fine as a placeholder for our player.

Adding a 3D as a placeholder for your player

Change the game background

If you are wondering why I have a solid background rather than the default one, you can change this easily as well. Select the main camera and then in the inspector you will see that it has a “Skybox”. This is useful with 3D games. But if you want a simple background then change it to “Solid Color” and select a background color of your choice.

Change your background to solid

Change the color of your player

If you like you can change the player to another color. We do this through “materials”. Under “project” in your assets folder add a new folder called “materials”. Then right click it to add your first material and give it a name. In the inspector you can now change the color. Then drag the material to the cube in your hierarchy window. The cube will now have a new color. You might also want to rename your cube to something like “player”.

Adding materials to your cube.

Move your player with user input

While it is nice to have your player there, it doesn’t do much yet. Let’s create some movements based on an input.

As a first step we need to create a C# script in the asset section and attach it to our player object. Use your favorite editor or if you have installed Visual Studio with Unity, you can simply double click on the script and edit it in Visual Studio.

We first want to position our player at the start of the game. Whatever is in the Start() method will be executed once at the start of the game.

If we say movement, we say speed. Let’s create a private variable for the speed:

We then create local variables for the horizontal and vertical input whenever the WASD keys are pressed. The Input.GetAxis(“Horizontal”) will move by 1 when the A key is pressed and by -1 when then D key is pressed. The Input.GetAxis(“Vertical”) moves by 1/-1 with the W and S keys.

If you are wondering why Unity “knows” we mean the WASD keys, then have a look at some of the predefined input methods. You can find them here Edit -> Project Settings ->Input Manager.

The last step is a “transform” which actually moves our object horizontally and vertically. It looks like this:

Whenever our input method, i.e. the WASD keys are pressed we move with the speed defined in the _speed variable in real time (Time.deltaTime), which means at 1m per second.

And here we are:

Right now the player can move beyond the actual screen. And this will be the challenge in the next post.

--

--

Andrea Zilio

Passionate Game Developer and Learning Expert. I love to create games and interactive experiences using Unity, Articulate, C#, JavaScript, PHP, HTML, CSS.