Simple Player Movement In Unity Part 2, and “Why You Need to be Writing Pseudo Code”

Bradley Yachimowski
4 min readApr 1, 2021

In part 1, we created a simple c# script to move our “Player” to “X=0”, “Y=0”, and “Z=0” when we started the program. Talk about a “Simple Player Movement In Unity”, well, it doesn't get much simpler than that. Let’s add some functionality to our “Player” script by adding a “W, A, S, D” navigation interface.

Before we start writing more C# code, we need to make some notes and something called “pseudo code”, which is a simplified programming language, used in program design. By using “pseudo code”, we can establish the names of variables we want to use or “need” to use in our code. Pseudo code allows us to construct a framework in logical terms in order to make our ideas clearer to others, who may also be working on the same project. Pseudo code can record our ideas, plans, and thoughts, and act as a reminder in the future. Pseudo code has many advantages and will help you to become a better programmer.

In the code above, you will notice the green “//”. This is used for remarks, and it turns everything on the line following it green. The “//” tells Unity that this line is not part of the program and to ignore it when we compile the program. Now that we have our goals established, and our pseudo code is written, let's start coding by opening the Unity “Input Manager”.

Now that we have the “Input Manager” open, let’s look at the “Horizontal” tab. Open the “Horizontal” tab by using your mouse to click on “Horizontal”.

Here we see the expanded “Horizontal” tab. We need to take note of the “Name”, which is “Horizontal”. Now we have a name…what do we do with it? Well, we can click on the “?” in the upper right of the “Input Manager” to research our next step.

So “float horizontalInput = Input.GetAxis (“Horizontal”);” is what we need to incorporate into our code. We can use the same format to get the “Vertical” axis as well. If you refer back to the “Input Manager” you will find the “Name” for the “Vertical” is “Vertical”.

Now, in order to move our “Player” we need to use “transform. Translate” in conjunction with a function called “Time.deltalTime”, which produces a one-second interval that is used to keep the speed constant regardless of the frame rate of our running program. The “Time.deltalTime” function may slow things too much; when this happens, we add a “variable” to multiply the speed of the “Player”. The “transform. Translate” function moves our “Player” in the direction and distance of translation. The “transform.position” function snaps our “Player” to the new position instantaneously.

Next, we set the limits of travel. To determine the limits, we simply drag our “Player” all the way to the left and then to the right; record the “X” values displayed in the “Inspector” window and then move the “Player” up and down and record the “Y” values displayed in the “Inspector” window. The range of travel in my “Game” window was, “X”= -9.24 to +9.24, and “Y”= -4.0 to +6.0. To enforce our “X” and “Y” ranges, we are going to use four “if” statements. We could use an “if-else” statement, but to keep this simple for the beginner we will use “if” statements.

When we run the program we find the object is moving too slow. We are going to change this by adding a variable called “speed”. This variable is going to be a type “ float” and we are going to assign a value of “4.5f” to the variable. We will then insert it into the “transform.Translate” functions to multiply the existing speed (which is actually a “translation” step size) of our “Player”.

I found that “4.5f” was the speed I liked, but feel free to experiment with different values. Now that our “Player” script is finished, you can go through and delete the “pseudo code”, if it is no longer a value to you. The last step is to drag our finished script into the “Scripts” folder.

This tutorial scratched the surface of what is possible. To take this a step further, check out the “Input Manager” documentation and try to control your “Player” with a joystick. Until next time…

--

--