Player Setup and more — Day 3 of becoming a Game Developer

Our Project has been created, the GitHub Repo has been created and we configured our layout for a better workflow. Now it’s time to start on our player.

The Player Object

To start creating our player, in Unity we’ll create a 3D cube in the Scene that will represent our player, for now, I’ve also gone ahead and named it “Player” so that we know what this cube represents.

After I’ve created my cube and zeroed it ((0, 0, 0) in the transform position on the cube). I’ve also created a “Materials” and “Scripts” folder in the “Assets” folder. I’ve then created a material in the “Materials” folder and named it “Player_mat” and set the “Albedo” to a light blue colour and applied it to my player object. Then I set out to create my player script and added the script to my player object.

Simple Player Movement

Thus far, we have our player object and everything it needs but it doesn’t “know” how to move around and that’s where we have to code its logic. I opened the Player Script and set out code basic movement where you would press “a” or “d” to go left or right, “w” or “s” to go up or down.

I’ve also added a line to my start function to ensure the player always starts at the zero position, regardless of where I may have moved it to while working on it in the scene.

The issue that arose is that the player could move off the screen or camera and we don’t want that, and we’ll work on fixing that in a minute.

Variables — The building blocks of programming

You may have noticed I have a private float called _speed. That is called a variable that you can adjust that variable in the inspector or from other functions or scripts. In C# there are 4 main types of variables:

  • String -Represent strings or blocks of text
  • Bool -Boolean which represents a true or false statement
  • Int -Integer which represents a number without a decimal point
  • Float -Represent a number with a decimal point. A float value will always end with an f. For instance, a value of 2.5 will look like “2.5f”.

A variable contains up to 4 parts. The first part is defining whether it’s a public or private variable. Private means it cannot be accessed outside of the script they reside in, and public means they are accessible everywhere. The second part is the type of variable listed above. The second most important part of a variable is a name. Without a name, you won’t know what the variable is for. It’s like an envelope without any address on top. You won’t know where to send the envelope to.

The last part of our variable is an optional value. Let’s say you have a variable that’s an int and the name is age. This tells us that we’re storing someone’s age. In my case, at the time of writing this article, my age is 34. My variable would look something like this.

public int age = 34;

Now seeing that variable I can easily find and refer to it anywhere in my script or project and I’ll immediately know that the variable is my age.

Variables are a very important and integral part of programming in general and especially in Unity.

Player Clamping

You may have noticed that my player can leave the bounds of the screen. In some cases, we might want that or we’d want the camera to follow the player.

But in this game, I don’t want the player to leave the screen bounds and In fact, I want to restrict it to a certain area of the screen. But, I also would like it to teleport if the player leaves the left or right bounds of the screen.

To restrict the player to the middle point of the screen and bottom edge of the screen I’ll use a Mathf.Clamp function.

The player is now restricted to the middle and bottom part of the screen, but we’re still having an issue with the player being able to leave the screen bounds to the left or right. I could also use Mathf.Clamp, but I would like the player to teleport to the opposite side of the screen like seen in PacMan and other games.

In the script above I’m using an “if” and “else if” statement to detect if my player has moved beyond the bounds I set and if it does it teleports the player to the opposite position. I think it’s a great way to get to the other side of the screen or “map” quicker.

That pretty much wraps up our player movement. During this article we’ve created our player object, added it’s materials and wrote the logic that controlled the player and bound it to a region we defined. The full script can be viewed here.

Self-taught Unity and C# Developer with a passion for games and the stories they tell.