Journey into 2D; Simple Player Movement (Game Dev Day 5)

Jack Leavey
4 min readJul 16, 2021

--

After an unplanned and unfortunate hiatus, we are back into the world of game design.

Today I covered making a controllable player object that can move and wrap around the screen.

Arcade 2D movement.

So how did we get here?

First off we need to create our player. For now I am utilizing a simple cube.

Creating the player object.

Rename this object as “Player”

You can change the color, light, etc. for the player object as you see fit.

Now lets make this thing move!

First we need a C# script.

Now we will open it our empty script so we can begin writing our movement code. Double click on your newly created script in order to pull up the syntax.

The default syntax of a C# script for Unity.

First we are going to set up our speed and some basic parameters.

Note: Anything you see in green text is pseudo code, it is NOT doing anything. They are just mental notes to help you as you write your own code. Feel free to utilize them or not!

This next chunk of code just tells the game that when we start the game, on the first frame put the player object at 0,0,0 (the center of the screen).

Starting Position.

Now we move into the update section of the script. Update means the script runs every single frame, so usually 60 frames per second.

The Update Section.

Now I have placed all of my controls for the player in its own void (or section) called CalculateMovement. You can either put all of the following code straight into Update, or make your own void to keep it nice and tidy.

Let’s get moving!

These lines of code are the utilizing Unity’s default naming scheme for player controls. Simply put, it means we can now move up, down, left, and right utilizing WASD or the arrow keys.

It’s Alive!

Now unfortunately we can still move off our own screen right now, and that would not be very fun for the player. To stop that from happening we are going to lock our player object in the area where we want the player to stay.

This nasty looking paragraph is actually very simple once broken down. It is utilizing 2 if and else if statements, one for each axis we want the player to move on (X,Y).

From the top:

If the position of the player on the Y axis is greater than 0, then move the player to 0 on the Y axis, keeping the same X value. This essentially locks the player from moving up on the Y axis.

The Else If statement is used to tell the script to use the same rule for the next segment as well. So:

If the position of the player on the Y Axis is less than or equal to -3.8, then lock the player at -3.8 on the Y axis, retaining the X axis value.

Save your code, and then drag and drop it onto your player object and try moving around!

--

--

Jack Leavey
Jack Leavey

Written by Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!

No responses yet