Time to get moving

Simon Leen
8 min readMay 10, 2021

--

Objective: Create a player and give it basic movement.

It’s game dev time. Time to get the character running, jumping, diving, rolling, hiding, gliding, WAIT! Maybe we’ll should start with up and down, left and right or is it forward and back!

This is 2D after all we’re working with. Which is left and right or up and down or forward and back. I suppose that depends on the games perspective and even more so when it comes to 2D.

Is the game a side scroller or is the game movement and flow from the bottom of the screen to the top. A simple concept but it will matter. For this project, the flow will be bottom to top so forward and back will be along the y-axis and left and right will be alone the x-axis. No z-axis because of 2D .

Now before getting ahead of ourselves, let’s just create our fancy player and put it in starting position.

Create our player
Player on the grid
Player on the game view

Next click the cube in the hierarchy and in the top of the inspector, rename the cube to Player and change the tag to Player. Renaming the cube to Player also changes the name in the hierarchy.

Renamed cube to Player

Taking a slight detour to make the Player and scene more visable. Skip down to “Continuing movement from here” to skip the visual aspects.

To make the Player(cube) more visable in the scene and more importantly in the game view, click on the Main Camera and in the second block of the inspector in camera, change the clear flags from Skybox to Solid Color.

Solid Color with Black Background
More visable Player in game view

To give the Player a bit more color, create and add a material to the Player. To start, right click the Assets folder in the Project panel and create a new folder called Materials. Having all the materials in this folder with make it easier to manage later.

Create Materials folder

Next right click the Materials folder and create a Material.

Create Material

Rename the Material, Player_mat. The _mat at the end of each material will make it easier to identify them later if they end up in wrong folders or mixed folders.

Player material

With the Player_mat selected, go to the inspector and click on the white box associated with the Albedo setting. this will pop up a color selector. Choose the color you want for the player. Once done drag the Player_mat on to the Player object in the hierarchy to assign the material to the Player.

Changing the material color
Adding the material to the Player
Player with material added

The player still doesn’t really pop so, in the hierarchy select the directional light and in the inspector increase the light intensity until you get the desired look.

Light settings
Player with higher light intensity setting

Now that the Player and scene are lit and colored, time to get into some movement.

Continuing movement from here.

To start working with movement we’ll need to set up a Player script. In the Assets folder of the Project panel. Right click the Assets folder and create a new folder named Scripts. Next right click the Scripts folder and create a new script and call it Player. try to rename it as it’s highlighted as renaming scripts can have some adverse effects if the script name and the class name within the script don’t match up.

Creating the Player script

Now that the script is created, drag the script on to the Player. Double click the script to open it in Visual Studio(or another IDE if you have set that as your default in the settings).

Player script

As we can see from the above image. When you create a C# script in Unity, the script is created with a basic template specific to Unity. The using aspects in the top of the script are the libraries the script is using. The using System.* items are part of the .Net(dotnet) framework that allow us to write some C# code. The MonoBehaviour is specific to the Unity Engine so without the using UnityEngine this would not work. MonoBehaviour gives us access to Unity specific methods such as void Start and Update. Start is called at the start of the game and Update is called every frame (typically 60 times per second).

The Transform component of any object defines the position, rotation and scale of that object. When moving or setting an objects position (in this case the Player), we need to access the Players transform. From the transform you can choose to change the values of the position, rotation and scale. When using positions we use Vectors. When using Vectors to assign position we need to use a new Vector. For this project, even though we are building a 2D game, we are using a 2.5D(3D) envoirnment so we will use Vector3s. We will set the Players new position on Start by setting the transform position to a new Vector3.

Setting the Player position on start

To move the Player we need to translate which way the transform will go. If we press right to move the Player right, we need to translate the direction of right to the Players transform so it knows which way is which.

If we set the translation without setting a time or distance to travel the Player with just infinitely go in that direction so we use specific logic to control how fast and far the Player travels.

Time.deltaTime translates the frames to realtime(just think of it as 1 meter travel for 1 second in the direction the transform is moving or translating).

To test this without input just to see the Player move, we can start with a right movement on Start. This has no set time so they Player will continuously move untill the game ends.

Right movement without setting Time.deltaTime
Player moves to position on Start and on Update flies off the screen
Right movement with Time.deltaTime apllied
Restrictive movement from Time.deltaTime

To increase the speed of the movement we multiple the direction by Time.deltaTime by and interger or float value.

Increasing speed of movement

Unitys project settings come with default attributes such as the axis. The X axis is available by using the Horizontal attribute and the Y axis as you can guess is the Vertical Attribute. Movement left and right will be along X and up and down movement will be along the Y.

Using these axis points is where the mathematics element of game development comes into play slightly but it’s easy to understand. If you place your Player dead center in a middle of a box you have the origin position(0,0,0). The three 0s in the brackets equate to position(x,y,z).

Take the X(Horizontal). At 0 you are center. To move right increases the value and to move left decreases. Y(Vertical) increases as you move up and decreases as you move down. Z in this case will always be the same because of the 2D logic of the game.

If we look at the previous code snippet we have Vector3.right being assigned. This right is on the X. Vector3.left would also be on the X. In the movement example we set Vector3.right to move by deltaTime by 5. As 5 is larger than 0, the Player moves right. If we set Vector3 by deltaTime by -5 the Player would move left.

The same would apply to up and down but along the Y. If we set Vector3.up by deltaTime by 5 the Player would move up(forward) or by -5 would move down(back).

To assign the movement to the keys we can use the arrow keys or WASD buttons on the keyboard. Up and down arrows or W and S keys will be on the Y and left, right, A and D will be on the X.

If we assign the Horizontal. No movement will be 0. Pressing left will be -1 and right will be 1. The Vertical no movement will be 0. Pressing up will be 1 and down will be -1. We can assign the Horizontal and Vertical using variables. We will use floating point values instead of integers as the multiplication of values will not always give whole numbers.

We will also set a speed variable to use within the method. This would generally be set before calling the Start and Update but for simplicity here it can be set in the update. Speed will be set to 3.5f (f being a float).

Then two more floats will be set. One for the Horizontal called horizontalInput the other for Vertical called verticalInput. This value will update on each frame depending on which keys are pressed. Then the transform.Translate will be updated to include these input values. Now on the start the Player will only be set to the position in the the Start method and will not move without user input.

Speed and input values assigned with updated Player movement

We now have two transform.Translate calls. One for the X and one for the Y. This can be further optimised for less calls and refactored into one transform.Translate using a single Vector3 call to assign both X and Y.

More optimisied Player movement call
We have Player movement

--

--

No responses yet