GS002: Making our Player move

Just yesterday, we successfully created our player. Okay, it might me a default cube for now, but want to keep things simple and basic for the beginning.
However, our player is static. We should give it the ability to move around.

A quick note before we start
For this article, we will be using the legacy Input Manager of Unity and NOT the new Input System package. If you are looking out for this and want to read about it on my blog (thanks!), then you will have to wait a bit longer. I will cover this topic, but not for now. Sorry!

Getting started
As mentioned in the previous article, we want to keep our Hierarchy tidy. Therefore, as we need some scripts in the game, go ahead and add a new folder called Scripts. As the name might imply, this will be the place where all of your scripts will be stored.

Forgot how to create a folder? No worry, this can happen, especially if you’re at the very beginning of your learning journey.
Right click the “Assets” folder > Create > Folder > Name your folder

Creating the script
Right-Click
the Scripts folder > Create > C# Script. Name your script “Player”.
It is important to know, that you should rename the script right after you have created it. Do not click away and try to avoid renaming the file later on. Otherwise, you’ll need to edit some things inside the script file itself.

Drag the just created script on top of the “Player” object in the Hierarchy window to attach the script to it.

Double-clicking the new file should launch your preferred IDE. If not, you might have skipped a bit of this article here. Just scroll down to the very bottom until you see the GIF in the “Settings you should take a look at” section ;)

The boilerplate code of every script
Now, as your script has opened, you should see these lines of code:

using UnityEngine;

public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

Now then, what are this lines doing exactly?

  • using UnityEngine
    Simply put, this allows you to use methods which are Unity exclusive. Without it, you would get an error as soon as you type in a Unity-specific method.
  • public class Player : MonoBehaviour
    Okay, we put this down into little pieces to make it easier.
    1.: public means that every other file can access your class.
    2.: Imagine a class as a prototype from which objects are created. Basically, a class combines methods into a single unit and makes them accesible for other scripts using the same class.
    3.: Player - The name of our script
    4.: MonoBehaviour - This is necessary to be able to use Unity functions like void Start().
  • void Start()
    Everything inside this method will be called and executed at the first frame. So, simply put, just once directly after you have started your application.
  • void Update()
    Every line of code in here will be executed in every single frame.

How do we make our player move now?
First of all, we should think about when we need to check for any type of input. We can change our direction at every time, therefore, we need to write the movement code inside the void Update() method.

We need a variable, which controls our speed. Therefore, let’s add this line of code right before the Start() method:

[SerializeField] private float _speed = 4.0f;
  • [SerializeField]
    This allows Unity to read the value of this variable and display it as an editable field in the editor, allowing others to tweak settings, without having to change anything inside the code.
  • private
    Unlike public, private variables can only be accessed and changed from the the script where the variable was declared in (in this case the Player script).
  • float
    This is the data type of the variable. float allows you to use comma-seperated numbers like 4.5 for example.
  • _speed
    The variable name. Keep in mind that you should name your variables with remarkable names, so you instantly know what the value of the variable will change. speed is better explained as myVar, isn’t it?
    The underscore in front of the name helps you to know that a variable is private.

Input Axes

Edit > Project Settings > Input Manager reveals all the Axes Unity allows you to access. Don’t be overwhelmed by that amount of axes, we just need two of them. Horizontal and Vertical to be exact.

Getting values from these in our code is pretty simple. We jst need to add these two lines of code inside the Update() method:

void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
}

With these two lines of code, we’re defining two variables of type float and telling them that they are equal to the return of the method GetAxis.
Horizontal and Vertical are the two axes we want to access. Therefore, we write the name of the axes behind GetAxis in the brackets.

However, our cube won’t be moving just because of these two lines. Nothing will happen when you run the application to be exact.

Why?
We might have accessed the Axes, but Unity does not know what you want to do with them. Let’s add some code to change this!

Let’s break the newly code down in single pieces:

  • transform.Translate
    This is a Unity method that tells the object’s transform (or position) to change (or update) according to the values provided by the float values we created.
  • Vector3.right / Vector3.up
    Vector3.right is shorthand for Vector3(1,0,0) . The numbers here refer to the x, y, and z axes.
  • Vector3.right * (horizontalInput * _speed * Time.deltaTime)
    Our horizontalInput is a float between -1 and 1. Pressing the A key will result in a value of -1, where pressing the D key will report a value of 1 back.
    _speed is a variable we set as a float with the value 4.0f.
    Time.deltaTime will normalize the movement speed. Because of that, we’re not instantly zoom out of the screen.

Moving!
Save your file, go back to Unity and run the application. You should now be able to move the cube around by using the WASD keys on your keyboard!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Timo Schmid

Timo Schmid

46 Followers

The mission? Becoming a game developer! RPG is the dream! Writing down my journey here for me and for everyone interested. Thanks for showing interest :)