First Steps: Player Movement

Edward Kim
4 min readMar 25, 2021

Now, there are many different types of games out there, but I am currently trying to build a 2d shooter. The first thing that I would need to do, is allow the Player to move. It would not be much fun if you could not dodge, or maneuver against enemies trying to shoot at you would it?

So, how do we go about doing this?

First, let us make our Player character which for now is a 3d Cube (spruced up a little with some color) and lets make the background on the Main Camera into a solid color.

Next we create our first script in the Scripts project folder and name it Player as it is going to deal with anything that is relevant to the Player Character.

Opening up the script I see that there is a void Start, and a void Update. Void Start checks the code at the start of the application, and Update checks every frame. Typically a computer runs at 60 frames per second. So we will need to put our code within void Update as we need to constantly be checking for the Player’s movement through the game.

I know that I can move an object using transform.Translate(new Vector3(x, y, z) and multiplying the Vector3 by Time.deltaTime so that it checks the code in Seconds, but I want to move my player by the controls on my keyboard. How do I associate this with the script. Luckily, Unity has an easy way to do this and it is located in the Edit/Project Settings/ Input Manager at the top left.

In the input manager, it shows me that for Horizontal, it uses the buttons left, right, a, and d. If we go to the Vertical section, it will tells us up, down, w, and s. Perfect, now what do I have to do in the code?

Well, first I have to make a float variable that will hold my Horizontal input. In reality, all the Input is doing is returning a value of 1 or -1 to determine if you are going left or right, up and down. For whatever input you use using, in the Input.GetAxis(“Input”) it must exactly match what is in the input manager and as it is a string (text), must be inside quotes.

Now we can change our transform.Translate from before, to this new one here. We say that the Vector3 is whatever our horizontal axis is for the X (so either 1 or -1 depending on what we hit), our Y will be whatever our vertical input is, and our Z as we have no need for it, will just be 0. Multiply this by Time.deltaTime to make sure it is checking in seconds, save, and then start up the game….

PERFECT

I hit A, the cube goes left, I hit D, it goes right. W and S make it go up and down, and we are on are way to making a full game!

Baby steps.

With this the movement is done (for now) and well next on the to do list is to have it shoot some lasers. Need to shoot enemies in a 2d shooter right?

Every little thing I learn feels as if all these ??? sections in my knowledge from working on games as an artist are filling in. It is quite a great feeling to finally understand how this all works and I am pumped to learn more and more.

Thank you for following, and I will see you next time.

--

--