Basic 2D Player Movement in Unity

Gabe Gomez
7 min readApr 26, 2021

--

This article will be a quick tutorial on how to set up some basic movement within Unity by manipulating a GameObject’s Transform component through code. This tutorial is better for those that are beginner programmers and those who have just started working with Unity.

** For this project, I am assuming you have a good understanding of how to navigate the Unity Editor, including the creation of GameObjects, materials, scripts, and other files. If you are unsure how to do these, check out my tutorial Installing and Getting Started with Unity which covers these topics.

The Breakdown

This tutorial will be broken up into six major sections, where one of the sections is optional. Feel free to jump to whatever section(s) you actually need help and an explanation on.

  • Project Setup (optional)
  • Setting a Starting Position
  • Moving the GameObject
  • Varying the Speed
  • Implementing User Input
  • Set up Bounds and Restrictions

Follow me along in this tutorial and you should have some basic player movement set up and ready to go in an hour or less!

Project Setup (optional)

Before getting started with this tutorial, let’s set up our environment of objects to manipulate and play with. If you are going to follow this tutorial in a project you already have going, feel free to skip this section. If not, let’s create a fresh project through Unity Hub (I am using version 2019.4.4f1), and feel free to name it whatever you want (the name of my project is “BasicMovement”). For this tutorial, I’ll be using the Universal Render Pipeline (URP) template. Once the project has been initialized and the Unity Editor has appeared, you should be greeted with something similar to the image below:

Editor Layout Tutorial can be found by clicking on this caption

Delete the “Example Assets” GameObject, and set your “Main Camera” GameObject’s Transform position to (0, 0, -10), and also change its rotation to (0, 0, 0). Once done, spawn a cube into your scene, name it “Player”, and set its Transform position to (0, -3.5, 0) (or wherever the GameObject is closest to the bottom of the Game View). Feel free to give this cube a custom material with whatever color and settings you want. You should get something like I have shown here:

Now you may want to change the background so your GameObject stands out. Optionally, to fix this, you can select your main camera, and within its camera component, there is a section named “Environment”. Change the “Background Type” to “Solid Color”, and feel free to play around with the background color until you get a contrast you like. This is how my changes came out:

Nice! We’re all set up with what we need to continue to the rest of the tutorial. Now, let’s go over scripting and how to set a starting position.

Setting a Starting Position

Now, in this tutorial, we will be manipulating the GameObject through code. As such, the first step we need to do is create a C# script. Feel free to name this C# script whatever you like, but I will be naming mine “Player”. Attach this “Player” C# script to your “Player” GameObject in the scene. Within the Start() method of your script, set its position with the following code:

The following code will set the GameObject’s position to (0, 0, 0) in global/world coordinates. Save your script, hop back into the Unity Editor, and hit the “Play” button. You should see your GameObject has been sent to (0, 0, 0) in the world coordinates.

Showing our GameObject being sent to ( 0, 0, 0 )

Setting a GameObject’s position through code has many use cases, and it is something we will expand upon in the next section.

Moving the GameObject

In the previous section, we set the position of the GameObject, and that was about it. Now let’s expand upon this idea and set the position of the object over time so we can see some movement. Delete the code we wrote in the Start() method and in your Update() method, type in transform.Translate( Vector3.up * Time.deltaTime). If done correctly, This will move your GameObject 1 m/s in the up direction of the world axis like I am showing below (in “Play” mode of course in the case that was not clear):

Now let’s get to changing speeds.

Varying the Speed

Now in most cases, you probably want to be moving faster than 1/ms. Sometimes you want to move slower than 1 m/s. There are a ton of reasons you’d want to change the speed, and right now we’ll go over how we can do that. Hop back into your “Player” script, create a public float variable named “speed”, and set its value to two (2) for now. Then, change the transform.Translate( Vector3.up * Time.deltaTime) line to transform.Translate( Vector3.up * Time.deltaTime * speed). After these changes, your code should look like the image below:

If you go back into Unity and go into play mode, you should see the GameObject move at a faster speed. Not only that, but you are also able to change the speed variable while in play mode through the Inspector in the Unity Editor. A positive speed value will make the GameObject move up, while a negative speed value will make the GameObject move down as I show in the GIF here.

Now that we have the ability to change the speed of a GameObject and the direction it can go in the world’s y-axis, let’s try to get some user input going.

Implementing User Input

Adding in some user input should not be too bad now. Open up your “Player” script again and change up your code to look like the following code in the image below.

As you can see, I only added another value to be multiplied within the transform.Translate() method, where this value is Input.GetAxis(“Vertical”). Input.GetAxis(“Vertical”) is part of Unity’s Input Manager system, and by default is mapped to the “W” key for a positive value in the vertical direction, and the “S” key for a negative value in the vertical direction. If you ever want to check out and/or edit your Input Manager system for your project, go to Edit -> Project Settings -> Input Manager. If implemented correctly, you should now be able to move your GameObject in the vertical direction with your “W” and “S” keys while in Play mode, as I have shown here.

Finally, let’s set up some bounds and restrictions to ensure our GameObject can not travel outside of our screen.

Set up Bounds and Restrictions

To set up the bounds where the GameObject cannot pass, I recommend you select the GameObject and move it vertically until the top of the cube is at the top of the Game View. Take note of the GameObject’s Y position value in its Transform component. Now do the same thing, but now with the GameObject at the bottom of the Game View window. Open up your “Player” script and add change up your code to look similar to how I have it here:

Note that I am now capturing and storing Input.GetAxis() within a variable. Just for best practice reasons

The only thing that you may have to change in this code is the topBounds and bottomBounds properties to match the values you just collected for this step. If all is done correctly, you should have a result that looks like what I am showing below (by the way, I cranked up my speed variable to eight (8) at this point):

Conclusion

Awesome! You got through this tutorial, which means you should now have some basic movements in the vertical direction, where that movement is controlled by user input and a speed variable, and where the GameObject can’t go off the screen. As a challenge, try to add horizontal movement and horizontal bounds to have movement in both the x and y axes. On my end, I extended my code to include movement in both directions, as well as coding in a boost implementation.

Today’s ending point

Starting tomorrow, I will begin writing articles on my 2D Game Dev Journey, and I will use this project as a starting base. Comments and critiques are welcome. If I made an error, please let me know so I can edit it for future readers. Have a nice rest of your day (or evening)!

--

--