Day 4: Simple Player Movement

Blake Zoeckler
4 min readApr 23, 2021

The time has finally come to start actually creating something in Unity. Something… ALIVE! Or, at least something that moves on it’s own.

Lets start simple and create a square that will moving around. It’s always a good idea to use simple shapes to prototype the game before adding more complicated graphics.

You can see the square is added to the Scene view, Game view, and the Hierarchy.

We can also rename the square to “player” to better keep track it what it represents in this game.

To make it move we will need to use a script. Unity uses scripts written in the C# programming language in order to control all the behavior of the various objects that are added to the scene. First we will want to create a new script file and rename it to “Player”. Next, drag the script onto the player, so that our player will be affected by it.

Now lets double click the Player script, which will open up the IDE to edit the code (Visual Studio 2017 in my case). Say we want to make sure that it’s position starts in the middle of screen whenever we start the game. We can add a line of code to the “Start” method, which will execute when the game is started. Here we add transform.position = Vector3.zero.

I’ve added lines 10 and 11 into the “void Start()” method. Line 10 is just a comment telling us what is going on here.

This line of code does utilizes the objects “transform”, which is a structure Unity uses to keep track of various spacial properties of the object, like it’s position, rotation, and scale. The dot operator “.” is the C# way to access information from that structure. In this case we use it to access the “position” of the object. Next, the “=” is used to assign new information to that position. In this case, we assign the position to “Vector3.zero” which is a shortcut way of getting the center of the world, at coordinates (0,0,0).

After making changes in the code, we have to tell the IDE to compile by using the build command like so:

You can also use the hotkey Ctrl+Shift+B.

Now we go back to Unity. When we click the play button at the top of the screen, you can see the square move to the middle of the world, since that where is considered the “location zero” that the player is moved to by the script.

It takes a few seconds for the game to start when you click the play button.

That’s nice, but what if we want the square to move while the game is running? That’s where the other method “Update” comes into play. It automatically and repeatedly executes any code in it as the game runs. Lets add another line of code into here:

Lines 17 and 18 are new.

This time, the position is added to by using the “+=” operator, instead of setting it to a certain value. We add “Vector3.right” to move this object right, and multiply it to “Time.deltaTime” which is the small fraction of the time that passes in between updates. This helps keep the movement steady, since the distance moved will always be proportional to the amount of time that passes.

After building the solution again, lets try playing the game:

And there we have a little square that moves on it’s own. Of course this is just the tip of the iceberg; in coming articles we’ll expand on this greatly and allow control over this square by using the keyboard as input.

--

--

Blake Zoeckler

I’m a passionate and talented software engineer seeking an opportunity in game development.