My 90 day journey becoming a Unity game developer: Day-2

Rhett Haynes
3 min readMay 20, 2021

--

Objective: Creating borders for the player to be confined within. The player will move to the middle of the screen and to the bottom without going off the screen along the Y-axis. While we will move the player along the X-axis where they will go out of the screen on one side and come back in on the other side in a wrapping manner.

The final result of restricting the player along the X and Y axis.

Inside Update(), we will create the following code restricting movement along the Y-axis:

Player being restricted along the Y-axis.

Next, we will restrict movement along the X-axis. Instead of restricting the player from going out of bounds, we will make the player wrap around to the other side of the screen. Here is the following code to make that happen:

Wrapping around the X-axis

We can optimize this code using the Mathf.Clamp() function along the Y-axis. The Clamp() function uses the minimum and maximum values to keep the player within those boundaries. We can’t use this code along the X-axis because we need the player to wrap around one side of the screen to the other. Use the following code:

Using the Mathf.Clamp() function to optimize the Y-axis movement.

We will take all the code inside Update(), and put it inside our new function called PlayerMovement() like so:

Created PlayerMovement() function.

Finally, use the PlayerMovement() function inside Update() to control the player’s movement.

Next time we will create a shooting prototype to give the player the feel of a spaceship fighter. Until then … see ya!

--

--

Rhett Haynes
Rhett Haynes

Written by Rhett Haynes

Learning to become a Unity game developer.

No responses yet