2D Space Shooter 2: Player Movement

Jeff Minnear
4 min readMar 22, 2021

--

Continued from part 1.

Now that you’ve installed Unity and created your project, it’s a great time to set up version control. After you do that, open up your project and create a new scene (File > New Scene).

We’re going to jump right into implementing our game logic, so it’s time to create a player and make it move!

In the Hierarchy panel, right click and choose “3D Object > Cube”. Name it “Player” and make sure to reset its Transform so it will be in view.

Click the “Play” button and get ready for some mind-blowing cube action as it… just sits there. Ok, so how do we control it? In the Project pane, right click on Assets and create a folder called “Scripts”. We’ll store all of our game’s code here. In that new folder, create a C# script called “Player”.

Open the new script in your text editor of choice (I’m using VS Code, but any editor will work). You’ll see that Unity created some code for us.

The most important thing to note here is the Update function.

Update is called once per frame

Well that sounds like a great place to handle movement!

Unity has a built-in Input system that we can take advantage of to add both keyboard and controller support for player movement. Add the following code to the Update function.

float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");

Surprisingly, that’s all you need to capture player input. The first float covers the left and right arrows, the “A” and “D” keys, and horizontal input from the left stick on a controller. The second covers up and down, “W” and “S”, and vertical input on the left stick.

All that’s left is to apply that information to change the cube’s position. We can do that using Vector3s and the Transform’s “Translate” method.

Vector3s are super important in game development, but they really just store values for the 3 axes (X, Y, and Z). Since this is a 2D game, we don’t really care about the Z axis and can just use a 0. “Time.deltaTime” is just the amount of time in seconds that has passed since the last frame. Multiplying our direction by this value essentially decouples it from frame-rate. This way, the player moves at the same speed regardless of whether the game is running at 30FPS, 60FPS, or 1000FPS.

In order to see our script in action, we’ll need to add it to the player object. You can do that with the “Add Component” button in the inspector.

Now, if you play the scene and press the arrow keys, your cube moves!

This is great, but you’ve probably noticed that it’s moving a lot slower than you’d expect for a shooter. We can easily fix this by multiplying the direction by a “speed” value. I went with 5.5, but you can adjust it to your liking. Here’s the full script.

If you’re wondering about the “f” in “5.5f”, it just signals to the compiler that 5.5 is a float. Not adding this creates some ambiguity and will generally result in an error.

And that’s it! We’ve got basic movement for the player object. Tune in next time when we’ll begin shooting lasers.

Continued in part 3.

--

--

Jeff Minnear

Jeff is a software developer and musician. He’s quite fond of red curry.