2D Shooter: Making the Player Move
Now that we’ve delved into some C# and the interface of Unity, its time to finally start making things happen! Why not start with how we make things move?
Well first off, we’ll want to create an object. On the taskbar in Unity go to Gameobject > 3D Object > Cube. This will create a cube. Now we need to give it a script. On the project tab under Assets, you can create a folder specifically for scripts. Right click this folder, and create a new C# script.
Since we’ll be attaching the script to the cube, which should also be renamed player in the hierarchy tab, a good name for the script would be player. Drag this script onto the cube, and it will automatically be added as a component.
Open the script, and let’s do some coding!
Let’s add a variable for speed first, it’ll come in handy in a little bit. We’ll probably need a number with a decimal, so let’s use the float data type. Name it speed, then declare it with a value of 10.5. (I found this to be a decent speed anyway.)
In the start function, let’s make sure that the cube always starts in the same place. To do this, we’ll want to tell Unity to change the object’s transform vector3 value. So the code will look something like this:
Since this is a 2D shooter, we’ll mostly be focusing on x and y coordinates.
Now let’s add code in the update function so that we can actually start moving the player. We’ll need to add two more variables:
These are also float data types, and on the right are the commands we use to access Unity’s axis inputs. This will allow us to use wasd, the arrow buttons, or a controller to be able to move the player. Unity will already have mapped the standard keys, but you can change them if you want to.
Next, it’s time to tell what happens when the inputs are active.
transform.Translate is what is telling the program that we want the object to move. Vector3 is what gives us the coordinates of (x,y,z), and we’ll want to match the axis with the proper variable, otherwise the movement might be a little confusing. The * is the symbol we use to multiply values in c#. We’ll want to multiply the vector value with the input, so if we’re not moving, we’ll be adding a zero to the equation, and zero multiplied with anything, is zero. If the translate value is zero, we won’t move.
That’s also why we need two separate lines of code, because if we want to move left and right, but not up and down, the y vector will have a zero and then make the whole equation equal zero. We won’t get very far with that.
We also multiply by the speed variable, this is how we’ll have control over the speed. The Time.deltaTime command tells the program we basically want to move the player by one meter every second. Without this, the default speed of the player would be moving too fast for us to use. This, along with the speed variable, will give us control.
After this, you should be able to move the player! Just hit the play button, and use the corresponding buttons and you’re good to go!
Happy coding!