Simple Player Movement in Unity — Part 1

In Unity, there are a few ways that we can make a GameObject move on our screen. These can range from moving a GameObject using user input by pressing a key or button on a keyboard or game controller, or by programming a GameObject to move on its own. We will be exploring these basic movements using a simple 3D cube over the next few articles.

To create our cube, right-click in the Hierarchy window, highlight 3D Object, and select Cube. We will then want to rename our cube GameObject to Player. If the GameObject we create is deselected before we rename it, we can always rename it by selecting the GameObject in the Hierarchy window, then renaming the GameObject in the Inspector window.

With our GameObject named Player created, we can now move it on the screen by clicking and dragging the Y-axis (green), the X-axis (red), or the Z-axis (blue). Alternatively, we can also move the GameObject by clicking the axis in the Inspector window and dragging until we reach our desired location. Currently, the Player GameObject’s starting position is center screen, which is X = 0, Y = 0, and Z = 0, or (0, 0, 0). For our purposes, we will need to move the Player GameObject to a different starting position at the bottom of the screen.

Next, we will want to program the Player GameObject to move and receive user control input. But to do this, we will need to create a C# script to attach to the Player GameObject.

We will also want to create a Scripts folder in the Project window by right-clicking on the Assets folder in the Project window (scripts are assets), highlighting Create, and selecting Folder. Then, we will rename this folder to Scripts since this folder will hold all our future C# scripts.

Next, we will create a new C# script by right-clicking on the Scripts folder, highlight Create, select C# Script, and immediately rename the script to Player since this script will be specifically attached to the Player GameObject. It is also important to note that the script’s file name must match the Public Class name within the script, or Unity will not be able to use the script. The Public Class name in the script can be verified in the Inspector window.

Now that our Player script is created and properly named, we will attach the script to the Player GameObject by dragging the Player script in the Project window onto the Player GameObject in the Hierarchy window.

Alternatively, with the Player GameObject selected in the Hierarchy window, you can drag the Player script into the GameObject’s Inspector window.

Now we can open the Player script in Microsoft Visual Studio which is installed automatically during the Unity installation.

When we open the script in Visual Studio, we are provided a couple of Unity-specific Methods (Functions) named Start and Update, thanks to UnityEngine’s Monobehavior library. The Start method is called at the first initializing of the script. The Update method is called every frame at a rate of about 60 frames per second once the script is initialized.

Our Player GameObject should currently be positioned at the bottom of the screen. For our first movement action, we will move the Player GameObject back to the starting position of (0, 0, 0), also (X = 0, Y = 0, Z = 0). To do this, we will need to access the Player GameObject’s position attributes with the Transform section of the Inspector window.

The line of code we will use for this is very hierarchical. In the Player script, within the Start method, we will write:

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

To break down this code, since we want to access the Player GameObject’s attributes, we use the Player script. We want the Player GameObject to start at the new position, so we will write our code within the Start method. Since the Position attributes are within the Transform section in the Inspector window, we write transform.position. Vector3 is essentially the position of the object and is usually followed by the X, Y, and Z axis coordinates. In this instance, we want the Player GameObject to move to a new position, so we will want a new Vextor3 position of X= 0, Y = 0, Z = 0.

Let us save our script, go back to Unity, and let it compile the script update. Now, when we play our scene, we will see the cube move from the current position at the bottom of the screen to the new starting position in the center of the screen at X= 0, Y = 0, Z= 0.

In the next article, we will look at automating the movement of the Player GameObject at variable speeds.

Dev Day 11: Simple Player Movement in Unity — Part 2

Dev Day 12: Simple Player Movement in Unity — Part 3

--

--

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