Making your GameObject move in your game

Simple Player Movement in Unity

Make your player move

Marc Frappier

--

  • Project setup …………………….……… ✔️
  • GameObject / Player Created …………. ✔️
  • Background and aspect ratio applied …. ✔️

Now it is time to make our player move.

We won’t be doing anything fancy in this C# script, but enough to get you interested. We will be writing a short C# script, apply it to our Player object and it will move. We won’t be able to control it just yet, but it is a start. Lets get to it.

First we need to create a new Scripts folder within our Assets folder to keep things organised. So, right-click the Assets folder in the Project window, select Create and then Folder. Name your folder Scripts.

Creating a new Scripts folder

NOTE: You should have Visual Studio installed on you computer. This could have been installed at the same time you installed Unity.

Lets create our first script

Right-click on the Scripts folder, select Create and then C# Script. Name it Player. It is important that we name the script at this point so that it coincides with the class name created by Unity. Otherwise you might get an error.

Creating a new C# script

Lets open the script in Visual Basic (VB) so we can start editing it.

Opening the script in Visual Studio

Now that our script is open, we can briefly analyze its’ contents.

New script in VS

With this blank script, we get two pre-made functions: Start and Update.

Whatever is put inside the Start function is called before the first frame update. Unity will always check this function first.

The Update function is called upon once per frame and the normal frame rate is 60fps (frames per second).

Lets start by making sure that when we start our game, our player appears in the center of the frame. Since this is something that will only be done once and at the beginning of the game, we will put it in the Start function.

First of all, lets add our Player script to the Player GameObject. We do this by either clicking and dragging the script over the Player name in the Hierarchy or having the Player GameObject selected and its’ Inspector showing, we can drag the script to the bottom where it says Add Component.

Adding script to Player as component

This script is now part of the Player GameObject.

All objects have a Transform property which shows up at the top of the Inspector window.

Transform Panel

We want our Player to start at position (x: 0, y: 0, z: 0). To move objects, we use the Vector3 command to set our origin.

From the Inspector window we need to access the position inside the Transform panel.

Now, inside the Start function, between the curly braces {}, we write the following code:

Resetting position to 0,0,0

Now, if our player was not centered when we created our game, this will move it to the center before anything else.

Lets Make The Player Move

Whatever code we write for movement must go inside the Update function.

We can make the player move to the right using two similar pieces of code.

Moving Player to the right

If we run either of these pieces of code, we will see that our player moves to the right. the problem is it moves way too fast.

Player moving to the right

The reason for this is, remember that the Update function is read at 60fps. That means that our player is moving at 60 m/s. A bit too fast if you ask me.

We want to be able to control the speed at which our player moves. There is a way, it is called Time.deltatime. This represents real time. So if we use the following code,

Controlling the speed with Time.deltatime

our player will move at 1m/s.

Better but now it seems a little slow. So, now that we can control the speed, lets speed it up a bit. Lets increase the speed to 5m/s.

By multiplying the movement by 5 and then by Time.deltatime, we are increasing the player speed x 5.

Movement of 5m/s

Well, there you have it. We were able to make our player move to the right. Now if you want to make the player move to the left, you would change the 1 in the Vector3 to -1 like so.

Making the player move to the left

We successfully made our player move to the right and then the left.

--

--

Marc Frappier

Poultry Scientist by profession, Computer Scientist at heart. After many years in the poultry business, I have decided to give my passion for technology a shot.