Movement is one of the core fundamentals of any good game. Whether it’s navigating the world on foot, piloting a spaceship, or moving enemies around your game it’s what gives games their interactive dynamism that makes them so much fun even when the player doesn’t move all.

In this tutorial I’ll show you the fundamental steps creating a simple movement in the context of a 2D Space Shooter called Atomic Shooter in same vein as other Shoot ’Em Ups such as Gladius, Galaga, or Space Invaders.

Jumping right in we’ll start from basics, open up your Unity project, and for best results start with a blank project. You can learn how to set up a brand new project here. Once your project is up consider setting up a Git to backup your work and you can learn to do that here.

Next right-click the Hierarchy and click “3D Object > Cube”, that should spawn a new cube. This should also work with a 2D object but primitives are easy to prototype with. Be sure to reset it’s Transform Position to (0, 0, 0).

Now we’ll create the Player script that will handle the C# movement code. Right click the Project folder and right-click the empty space and click on “Create > C# Script” be sure to name it Player (or whatever you like). If you accidentally press enter without naming it, just delete it and start again otherwise it will be misnamed in the script when we go to rename it in the project pane.

Be sure to add the script to the Cube Game Object in the scene. This is important as it will drive all the logic that governs our player character.

You can either drag it to the cube in the scene, drag it to the cube in the hierarchy, or click on cube and click Add Component at the bottom of the inspector and click on Scripts to add it there.

Next we’ll double click the Player script and it should bring up your Integrated Developer Environment (IDE) of choice. That can be Visual Studio or another one like Jetbrains Rider (my favorite) if not you may have manually do so and navigate to the script in the project folder.

Above is the full code snippet for player 2D movement, we’ll break it down below.

First things first, variables. We have two variables governing the movement.

The speed variable dictates how fast the player moves when they move. Vector3 playerPos dictates the position of the player’s new movement. Since you can’t directly modify a GameObject’s Transform you have to set it every frame.

Next is the Start method. First we zero out the playerPos so that Player always starts in a predictable location. Next we set the Player’s actual transform position to playerPos this step isn’t necessary but it’s good housekeeping.

Next is the Update method, this is where the real action happens. The Update method updates every single frame so we use that to update the playerPos every single frame by setting a new Vector3 using Input.GetAxisRaw(“Horizontal”) and Input.GetAxisRaw(“Vertical”) respectively. This is by default the WASD/Arrow keys on the PC in Unity but you can define those controls in the Unity Editor to be whatever you like.

The other half the equation is transform.Translate, this takes in a Vector3 to move the Player transform in the direction of the input every single frame.

However we need to modulate the movement with Time.deltaTime otherwise we get jerky stepped movement instead of buttery smooth movement.

The speed variable simply allows us to tweak the speed of the movement faster or slower since moving in small increments every frame really slows things down. Here is the movement system in action. If it looks a little different, I took the liberty of adding a simple spaceship sprite instead of the cube.

That wraps up on creating a simple 2D movement system in Unity, with that you have the keys to extending the system out to whatever you’d like. You can now add power ups that modify the speed such as a boost or slow down. Set in screen boundaries to prevent players from moving off the screen or wrap around so that they can dodge projectiles going from one side of the screen to the other. The possibilities are endless, happy coding!