My journey becoming a Unity game developer: Player movement setup for Galaxy Invaders game-pt.3

Objective: Creating boundaries for the Player prototype when moving around the screen. Also, refactoring the code for better readability and use in the Player’s script.

Player moving with boundaries included.

We want to create boundaries for the Player to keep them onscreen at all times, but limited to how far they can move around onscreen. For the up position on the Y-axis, we want to limit the Player to zero which is the middle of the screen. However, on the down position we want to keep the Player from going off the screen which will be around -3.8f. On the X-axis, we don’t want the Player to be restricted from moving off screen, but to go off screen and reappear on the other side of the screen.

Finding the boundaries for the Player to be restricted to.

To figure out how to keep the Player’s movement restricted to onscreen, let’s use pseudo code to figure this problem out. If the Player’s position on the Y-axis is greater than zero, we want to keep the Player at zero on the Y-axis.

Using pseudo code to figure out the Player’s boundary on the Y-axis.

For the real code, IF the Player’s transform position on the Y-axis is greater than or equal to zero, we want to limit the Player’s Y-axis to zero. However, our natural programming instinct would be to set the Player’s transform position to zero, or set the Player’s transform position to a new Vector3 with zeroes across all 3 axes. This will not work because the 1st solution Unity will expect all 3 axes to have values assigned as a Vector3. The 2nd solution has all the values of Vector3 assigned as zero which will create unwanted results when playing the game.

2 Y-axis codes that won’t work.

When you move the Player around the screen, notice the Player will snap back to zero on all 3 axes when the Player stops moving.

Player movements snap back to zero on the Y-axis when finished moving.

To fix this code, set the transform position to a new Vector3 with the Player’s transform position on the X-axis to be wherever the Player is located onscreen at the moment. Set the Y-axis to zero as this will prevent the Player from moving above this Y-axis value, and the Z-axis to zero since we’re not using this axis at all in the 2.5D game.

Player able to move along the X-axis, but restricted moving up to zero on the Y-axis.

Now the Player can move along the X-axis freely and when they reach the zero value on the Y-axis, the Player can’t move past this set value on the Y-axis. However, they can move along the X-axis still even though they’re restricted from moving up on the Y-axis.

Player moving along the X-axis, but restricted on the Y.

Now, let’s restrict the Player from moving off the screen below by restricting it at -3.8f which will keep the Player onscreen at the bottom. Using ELSE IF statements, let’s check if the Player’s transform position on the Y-axis is less than -3.8f. If true, set the Player’s transform position to a new Vector3 with the transform position on the X-axis to be wherever the Player is located. Then, set the Y-axis to -3.8f to keep them onscreen when they reach the bottom, and set the Z-axis to zero.

Player restricted from moving beyond zero up and -3.8f down on the Y-axis.

The Player is now limited on the Y-axis at zero and -3.8f.

Player moving with limitations on the Y-axis.

To make the Player stay onscreen when moving off screen on the X-axis, we will have the Player reappear on the opposite side of the screen. Create the same code setup from the Y-axis, but we want to check for the X-axis to be greater than 11f. If it’s true, set the Player’s transform position to equal a new Vector3 with the X-axis set to -11f to make the Player reappear on the other side of the screen when they go off screen. The Y-axis will be set to the Player’s transform position on the Y-axis where they’re located at the moment. The Z-axis will be set to zero.

Player able to move off one side of the screen and reappear on the other side of the screen.

The Player is now moving off the screen on one side and reappearing on the other side.

Player moving off one side of the screen and reappearing on the other side of the screen.

We can now clean up this code inside the Update() function by refactoring it to make it more readable for someone else to understand the code, and make it more maintainable to use in the Update() function. Create a new method name PlayerMovement(), select all the code inside of the Update() function, cut and paste this code inside of the PlayerMovement() method. Now inside the Update() function, add the PlayerMovement() method.

Placed all the code in Update() into a new method named PlayerMovement().

Another thing we can refactor is the Player’s movement on the Y-axis. We will still set the Player’s transform position to equal a new Vector3 with the Player’s transform position on the X-axis to be the Player’s current position.

However, we will use a math function to restrict the Player’s movement on the Y-axis. Use Unity’s Mathf class with the Clamp() method which uses a given value which is the Player’s transform position on the Y-axis. Then, the next 2 values used will be between a range of a given minimum value of -3.8f and a maximum value given of zero. This will return the given values if it’s within the min and max values, finally set the Z-axis to zero.

Refactored the Player’s X-axis movement code using Mathf.Clamp() method.

The Player is still moving along the X and Y axes as intended using the Mathf.Clamp() function.

Player’s movement working on the X-axis using the Mathf.Clamp() method.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store