Creating Player Movement

Katie Jeffree
3 min readApr 17, 2019

--

Adding player movement to a game is one of the most exciting things in game development for me. There are no pre-made components that I can add to control movement of a player like I did for collision detection and physics (see my previous blog), so I needed to add some code of my own to create this movement.

To add your own code to an object in the game, you have to add a ‘script’ to the object (which is a file that holds code relating to a particular object). I added a script to my player so that I could make the player move left and right when the left and right arrows are pressed respectively. I started by looking at Unity’s inbuilt method of Input.GetAxis(). This method allows you to input the direction of movement (for example, “Horizontal” for left and right, and “Vertical” for up and down). These axis names have default keys that it listens out for when they are pressed (for example, I used the “Horizontal” axis as it listens out for the left and right keys as default). If one of these buttons are pressed, it returns a number between -1 and 1 which indicates which direction the sprite should be moving. A number greater than zero indicates it should be moving right (as you move further right along an x axis, the numbers grow larger), and if it is less than zero, it indicates it should be moving left. This number gradually gets bigger the longer this button is pressed, so as to provide a smooth transition for movement.

I then multiplied the number given by the above method by the speed I wanted to give to my player (a value of 10 units per second in this case) and then by deltaTime (the time elapsed between each frame of the game) to get a speed per frame for the player. This results in a value between -1.6 and 1.6 per frame, meaning the player will move between 0 units per frame and 1.6 units per frame to the right or to the left. I could then input this value into a method called Translate, which takes in values for the x (left and right), y (up and down), and z axis (depth from the screen) for how much you would like to move an object. I inputted the value I calculated above for the movement in the x axis, and set the other two values as 0 as I didn’t want these to change at this point. My player can now move right and left!

Now onto getting my player to jump in the air. This requires a different method, because if I used the same method as above, the player would continue to move up the screen when you continued to hold the up button, which is not ideal! In this case, I found a method called AddForce which requires how much change to make in the specified direction (the y direction in this case), and what type of ForceMode to apply, which can be Acceleration, Impulse or Velocity Change. I needed to use Impulse, as it provides an instant force to the object, but also takes its mass into account, so will still act in line with gravity and the other physical aspects of the game. This means that an upward force will be applied to the object, but this will not be continuous. Just what I need!

I now have my player moving side to side and jumping! He looks a bit stiff though without some animation, so that will be my next step!

My player moving side to side and jumping!

--

--