Basic Player Movement in Unity/C#

We’ve got our Unity project set-up, connected to GitHub, screen lay-out optimized, and now it’s time to start creating!

First we have to create our first GameObject! This will be the Player, or at least a representation of our Player (for now).

Right-click in the Scene Hierarchy and select > 3D Object > Cube. Rename it “Player” in the Inspector tab to the right.

It’s also a good habit to develop to reset the Transform, so that everything is at 0 — easy to find in the scene editor window, and in the case of Empty Game Objects in the future, will help with some headaches.

Next, we’ll create a Scripts folder inside our Assets folder, and create out first C# script! Then we have to make sure we attach the new Player script to the Player GameObject, or else any code we write will not be performed on our Player GameObject.

Open up the Player script by double clicking on the script (either in the Project window (left), or the Inspector window (right).

The first code we’ll be writing will be straight to the point — getting User Input!

This is done by typing Input.GetAxis(“Horizontal”), and Input.GetAxis(“Vertical”), retrieving the x and y inputs, respectively.

  • The Input manager handles the X and Y inputs and by default maps them to W, A, S, D, or by your keyboard arrows!

We also have to make sure to assign a variable to each command, so that we can recall it later (very soon actually). These will use the float data-type variable — because the Input Axis ranges from 0 to 1, and we want the decimal points in between there as well.

This will be done inside the Update method, which is called every frame. In other words, it’s constantly re-running all the code inside of the Update method!

Now we have a reference to the User Inputs, but we haven’t actually used them yet. We’ll create one more variable, to store both of the Inputs in conjunction.

This will be a Vector3. Vector3’s require an (x, y, z). These are used in navigating 3D environments. For our purpose of a 2D game, we can leave the ‘z’ value at 0.

Our ‘horizontalInput’ represents the X axis, therefore fills in the X of the Vector3. The ‘verticalInput’ represents the Y axis, going in the Y value of the Vector3.

Next we’ll finally put these variables to good use!

This will be done using transform.Translate. This function/method requires a Vector3 to know which way it’s supposed to be moving the GameObject this script is attached to. This is why we created our directionInput Vector3!

Since the horizontal and vertical axis have a max value of 1, our player cube would be moving relative to 1 metre in the game every frame. This is a little too slow, so we’ll create a Global variable (can be used within the whole script, by any method) called speed, and it give it a value of 10.

We’ll make speed a public variable. So that we can see it in the Inspector, and because I know I want to add a Speed Powerup in the future, which will require us to interact with it outside the script.

We can then multiply our directionInput Vector3 by our speed, to give us a max speed of 10m/frame. “directionInput * speed

If you have an ancient computer, we can leave it at that. But most computers can do well above 60 frames per second (especially with something as lightweight as our current prototype). So that means you’re Player will be whizzing by at over 600m/second!! This also varies with every computer.

To remedy this we multiply our current function by Time.deltatime. This gets the time from the current frame and previous frame — giving a more stable output.

Speed can be seen in the Inspector, since it is Public.

These few lines of code will now let us control our Player Cube!!

Now that we have functioning code, all related to Player Movement, we’ll clean up our Update method by creating our own method underneath, and calling that instead of all the code. This makes it much easier to debug things in the future.

We’ve created out first GameObject, first Script, and have begun coding in C# within Unity! The first steps to becoming a game developer!

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles