Step 1 — Moving The Cube
For the first portion of the GameDevHQ course, I will be making a 2D space shooter. These are the steps that will take us there.
If “Hello World” is the mandatory beginner rite of passage for coding in general, personally I think “make a cube move a bit” is the equivalent for Unity.
So how might we go from this:

To this:

Well first we create a C# script for the cube we can call “Player”.
Next we fill in the Update method with the following:

There’s a bunch of things here so let’s break it down.
- Update() — This method runs every single frame a game is running, so about 60 times a second, corresponding to 60fps.
- [SerializeField] — this makes it so that the value of the _speed variable can be changed within the Unity editor but only for a designer’s purpose. Other in-game variables won’t be able to access it. This will be elaborated further in a future article.
- horizontalInput and verticalInput — these values come from the Horizontal and Vertical axes in Unity’s Input Manager (Edit → Project Settings → Input Manager → Axes). What this is for example is that pressing right arrow gives a value of positive 1, while left arrow is a value of negative 1.
- direction — a Vector3 variable. Vector3s are a data type for the x,y,z coordinates of the Unity world. Combine the horizontal and vertical.
- transform.Translate — this is where it all comes together. This changes the transform (coordinates) of this cube in a certain DIRECTION multiplied by a SPEED value, multiplied by a…
- Time.deltaTime — The issue with Update() is that because it’s running 60 times a second, if I tell the cube to move 1 unit (1 meter in-game) it will move 60 units in one second. What Time.deltaTime does is break down your movement into 60 tiny chunks so that when 1 second passes, the cube properly moves 1 unit.
And voila! The cube lives!

If you really want to be fancy and add things like a y coordinate restraint and making it warp across the screen left to right or vice versa you can add the following:

This is telling the cube to do things like
- if my y position is greater than or equal to 6, then make sure I can still move left and right, but no further up than 6
- if my x position is less than or equal to -10, then teleport me to the other side (x position 10) but keeping my y elevation
If you really want to be *fancy* fancy, you can use Unity’s clamp function for the y position and save some lines of code.

This says:
- Restrain the y movement of this cube between a minimum and maximum value
Today, cube movement. Tomorrow, world domination.