2D Galaxy Shooter — Player and Movement

Marcus Cohee
3 min readAug 5, 2021

--

Hello all!

This is my first of many posts about a game I am making called, “2D Galaxy Shooter”. I am using GameDevHQ’s guidance to get me through the many struggles of coding and learn how to make amazing games!

Quick note, No game is gonna look beautiful with sprites and textures in the beginning. We have to work through the prototype phase, so please bare with me through it.

Player

First things first, This is “Player”.

I just made a primitive cube and set its color to blue using a material.

Next, create a C# script and name it “Player”, then drag the file to over the player in the Hierarchy. This is where we are going to make our player move using WASD or your arrow keys.

But before that, we need to make a starting location for our player when you hit play and we will be using “void Start()” for it. This is used when you want something to start as soon as you hit the play button.

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

By using that code in start, we are setting the position to (0x, 0y, 0z).

Now lets go into movement!

Since movement will be done while the game is started and running, we will be using “void Update()”, which updates the game every frame. The tricky thing about this is that there are 60 frames per second.

To be able to move the player, we are going to be translating them horizontally and vertically.

Edit -> Project Setting -> Input Manager

Looking above, Unity already know what most people use for movement in their Input Manager, so that makes our lives easier!

float horizontalInput = Input.GetAxis(“Horizontal”);
transform.Translate(Vector3.right * horizontalInput * 3.5 * Time.deltaTime);

By making the horizontalInput variable and adding it to the equation, we can go right, left, or stop. Looking at the picture above again, its says that right is positive and left is negative. So Vector3.right will always be +1, 3.5 will be our speed, and horizontialInput can be either 0, -1, or +1.

Time.deltaTime is used to make us go 3.5 /second in Update.
Without Time.deltaTime, we would go 3.5 * 60 / second, that’s half the speed of sound!

For vertical movement, we can do the same thing but change a couple units.

float verticalInput = Input.GetAxis(“Vertical”);
transform.Translate(Vector3.up * verticalInput * 3.5 * Time.deltaTime);

Player Movement

This was how to make a simple player that moves at our command.

Thank you for your time!

--

--

Marcus Cohee
Marcus Cohee

Written by Marcus Cohee

I am starting Unity and learning the ropes as I go! Let’s take this journey together!

No responses yet