Basic Player Movement in Unity

Jacob Jones
3 min readJun 20, 2021

In this article, we are going to learn how to make your character move up, down, left and right. To begin, lets create a variable called speed! I am just going to be using a cube in unity as my player, so if you want to follow along feel free to do the same! I am creating a public float speed. See below as an example.

Next, to ensure our player is consistent, we want them to start at the same spot every time we begin our game. Do this by using the transform.position with a new vector3! We are going to add this under the void Start section. Type out “ transform.position = new Vector3(0, 0, 0); ”.

Next, we are going to use Unity’s pre made input movements that we can add to our code! To check out all the available options you have, in Unity go to edit, project settings, and then input manager and finally click Axes! From here we are going to be using the horizontal and vertical functions.

Now we need to add these functions into our code. We do this by linking the function with a variable in the void Update section. To make our player move to the left and right we will use the horizontal function. Begin by typing “ float horizontalInput = Input.GetAxis(“Horizontal”); ”. We are naming this function horizontalInput linking it to the actual horizontal axis in the project settings.

Next we need to actually create the equation to include our speed variable! This will allow us to control how fast or slow we want it to move using the A and D keys. Type “ transform.Translate(Vector3.right * horizontalInput * speed * Time.deltaTime); ”.

Once you save it, give it a go in the play tester of Unity!

Now that we have completed the horizontal movement, I would like you to try and add the up and down movement yourself! To give you a hint, the code structure is the same, you only need to change a few minor details! See the image below to see it finalized!

Hopefully you managed to get the up and down movement yourself! You could have also used down instead of up and left instead of right! Now go to unity and check out the results!

--

--