Space Shooter: A 2021 development story. — Player Setup.

In this series, we will be building a 2D space shooter game similar to the old arcade game Galaga. This story will have us setting up the player and getting them to move with the Unity input system.

First, open up Unity and create a new 3D project. Name it whatever you wish. Now we will create our primitive player by right-clicking in the hierarchy, go to 3D object, and select cube. This will place a cube object in your scene view. Now rename the object to ‘Player’.

Now we are going to change the background of the scene, just for visibility. Select the ‘Main Camera’ game object in the hierarchy and select the ‘Clear Flags’ drop down and change it to ‘solid color’. Now you can change the entire background color. For this project, I will change my background to black.

We will also change the color of our player object. First, in your game assets folder, create a new folder and name it ‘Materials’. Now in that folder, you can right-click and create new material. Rename the file to blue_mat. This format is for the color you will be creating and the ‘_mat’ is to assist in quickly locating your material files when your project gets bigger. Once you rename the file, go to your Albedo setting in your game object setting on the right and click the white block. This will open your color picker and you can select your blue.
Now you can drag your new material on your player object in the scene or in the hierarchy. Now your player will be blue.

We’re ready to get this cube moving now. Next, we are going to create a new ‘Scripts’ folder and inside that folder, a new script called ‘Player’. Place the script on the player object by dragging and dropping it, then open the script and we are going to force our player position to a specific location regardless of where it is inside the engine.

This line will set the position of the game object to 0,0,0 in the scene.

We now want to move the player up, down, left, and right on the screen. First, we need to define the input keys, and Unity has predefined keys that we can access in our script. We want to do this inside a new function that we will call ‘CalculateMovement’.

Next, we want to apply movement to when these keys are pressed. To do that we need to define a variable and we will call it ‘speed’. We need to place this variable above the start function.

The ‘SerializeField’ header will allow this private variable to be visible in the inspector but also hidden from other scripts. The underscore in front of speed is to define that the variable is private. Once that variable is set, make sure to set a value to it inside Unity. I have mine set at 5.

The next two lines of code are going to get the player to move while playing. We’re going to access the player's transform, and use the ‘Translate’ public method to actually cause the player to move about the screen. We will then use ‘Vector3.right’ to move the player along the x-axis. We will multiply that Vector3 by our horizontal input variable and multiply that by our speed variable, then multiply all that by real-time which is defined in Unity as Time.deltaTime. To get the player to move up and down, we will do the same thing, except change Vector3.up and the input variable to our verticle input. It will look a lot like the following.

Seeing this and reading what I mentioned above will help you understand the process of getting the player to move.

Another route we could do to achieve the same thing would be to create a new Vector3 called direction and set the arguments to our inputs on the x and y, and set the z input to 0. Then we can call our game objects transform and Translate the Vector3 direction multiplied by speed and real-time. A cleaned-up version looks like this.

Awesome job so far. We now have a moving player that we can control. Let’s expand on this just a little. Let’s constrain the player not to go above or below a certain area when playing.

We don’t want the player to move above 0 on the y-axis and not move below -3.8 on the y-axis. To achieve this, we need to create an if statement with an else if. We want to

This is the result of this statement.

We can achieve the same thing in a slightly more optimal way by clamping the y-axis between those two positions. It would look like this.

Next, we’re going to set the player to wrap to the opposite side of the screen when they reach the maximum point. For example, the player moves completely to the right of the screen, they will then appear on the left screen. This is called ‘screen wrapping’. To do this, it’s the exact same as the if/else if statement we did earlier, except we’re working on the x-axis.

In this code snippet, we defined the maximum point when the player moves right as 11.3. When the player exceeds that point, they will appear at -11.3 which is the opposite side of the screen. We also reverse this in the else if portion of the statement. These statements allow the player to seamlessly transfer from one end to the other along the y-axis as input is being pressed.

That’s it for player movement. You can create your own primitive object, write a basic script to snap it to a starting location, move it in all directions, and constrain or wrap its position. Nice job.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

6 Hidden Pitfalls of Magento and how to Solve Them

Why Flutter is the Future of Mobile App Development

Why Flutter is the Future of Mobile App Development

How I learnt Design & Programming on YouTube.

I am data normalization- Let me help you to remove redundancy

How many DynamoDB RCUs are consumed during Query?

Demystifying Observability

My Serverless Frankenstein for Honeypots

Flutter Fast, Flutter Far — Issue #1

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
Chance Cornell

Chance Cornell

More from Medium

Week 11 — Open Source [2021/12/27]

Day 17 of my Developer Journey: Creating the Speed Boost Powerup

C# desktop application to connect to BLE devices using BleuIO

Singleton Design Pattern