Simple Player Movement in Unity

Ameen Mohmand
5 min readJun 3, 2022

Objective: A break down of Simple Movement in Unity.

Today let’s dive into the mechanics of simple player movement in Unity. For starters let’s get familiar with the components of movement. Every Game Object has a Transform and every Game object has a transform, you can use this interchangeably. Now that we know what a Game Object is and a Transform ,lets start by making our very first Game Object which will be our player. Lets start by going to Game Object in the top Left corner and click on it will give us a drop down menu, select 3D object choose the cube. Once you've done that you will see the cube appear in you ’re Scene view, now lets rename it player. You see all this below in the video gif.

One more way to a add 3D object in Unity is in the Hierarchy, you can click on the plus sign and it will give you a shortcut like when we navigated to the top left and click on Game Object. Another way to rename an a Game Object is through the inspector , just click on the 3D object name and rename it Player. Below in the video gif you will see how we do it.

The next step lets make some materials to color our player in our scene. To do this let’s start by making a materials folder. The next step is right click on the Project pane and a drop down menu should appear. Once the drop down menu appears select Create and folder . This will create a folder in the project pane while its still highlighted rename it materials. The next step will be to right click on your materials folder and select create from the drop down menu , followed by materials. It should show under your materials folder a highlighted New Material rename it Player underscore mat . Once you have done that lets change the color of the player by clicking on Albedo in the inspector and select a blue color from the color wheel . Then you click on your player_ mat and drag the color on to the player in the Game Scene view.

One important thing that is a must is Aspect ratio in Unity . The reason for this is your projects will have to fit all screens with regardless different sizes. To do this we will change the Aspect ratio to 16:9 in Unity. The correct way to do this is by clicking on Aspect in the Game View and choosing from the drop down menu and choose 16:9 Aspect. We will break down for you in the video gif below

Now to get our player moving in Unity we need to create a C# Script folder in our Project Pane . First lets right click and select create folder and rename it Scripts. Once that is done lets right click on the Scripts folder and select create C# script. While it still highlighted rename it Player because if you click off it will now register the default name NewBehaviourScript and it wont match the file name in your C# Script. Its important to name your script properly the first time.

Next let’s start by opening up our C# script first in Visual Studios. In void start lets move our player -by resetting the transform. Position in Unity with a new Vector 3 which gives you 3 over rides on the three axis x, y, z, . Then lets you change the y axis value to -2 in the inspector this helps with resetting our player in our scene .

In this next section where gonna get our player to move at a normal speed . Let’s break it down in our C# script by using transform.Translate which means move the transform in direction and distance of translation. So in void Update were gonna type transform.Translate(Vector3.right); This will cause our player to shoot quickly off screen to the right at high rate of speed. The reason we shoot off to the right at such a high rate of speed is because in void update its being called once per frame at 60 seconds . In addition we are moving our player 1 meter per frame to the right too. So we need to actually get our player to move 1 meter per second instead of 1 meter per frame. To do that we need to incorporate real-time so gonna need to write in our C# script transform.Translate(Vector3.right * Time.deltaTime); in void Update. Also this allows us to use real-time in Unity .

On the next section lets focus on User Input for our player in Unity and utilizing the WSDA keys. Lets go into our C# script and lets make our player move using a variable for our speed. Lets make the speed value a float variable also it make public so it shows in the inspector like so, public float speed = 3.5f. Now in Unity lets go up to the top left in Edit and scroll down to project settings and select Input Manager and axis. Here you can see the horizontal axis and vertical axis also what keyboard key’s both associate with which axis. Next in void update lets type out our code to make our player move with our speed variable. Next let’s make a public variable for our horizontal input and vertical input. Now that we have our input lets add them to our code in void update and move them up down with WSDA keys horizontal and vertically. This how you get simple movement with your player . Watch the video gif for the breakdown as well for visual aid.

--

--