My 90 day journey becoming a Unity game developer: Day-2
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.
Inside Update(), we will create the following code restricting movement 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:
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:
We will take all the code inside Update(), and put it inside our new function called PlayerMovement() like so:
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!