Keeping the Player on the Screen

In this article, we will make it so the Player will not go off the screen by putting limits on how far the Player can move away from the starting position.

Example of Final Goal

First, let’s make it so the Player can not go higher than the screen’s middle. To keep the Player from going above 0, we will set the Player’s y position back to 0 when it goes beyond it. In void Update, create an if statement that checks the Player’s y position to see if it is greater than 0. Inside the if statement, set the Player’s position equal to a new Vector3 with the x being the Player’s current x position and a y of 0. You set the x to the Player’s current x position instead of 0 because setting it to 0 would cause the Player to snap to 0 on the x if it was not already at 0.

Making the Upper Y Limit

With the Player no longer able to go higher than the middle of the screen, let’s make it so they can not go off the screen’s bottom.

Upper Y Limit

We will use an else if statement that will only execute if the proceeding if statement did not. We are using this instead of just another if statement because both are checking the same value (the Player’s y position). Under the if statement, create an else if statement that checks the Player’s y position to see if it is less than -3.8. Then inside the else if statement, have it set the Player’s y position to -3.8 and keep the x position the same as the Player’s current x position like above.

Now that the Player can not go off the bottom of the screen let’s clean up the code to make it more streamlined.

Up and Down Limits Working

To streamline it, let’s combine the two if statements by using Mathf.Clamp to clamp the Player’s y position between the minimum (-3.8) and the maximum (0).

Cleaning Up the Y Limit

In void Update under the y movement clamp, create an if and else if statement. Make the if statement, so it checks if the x position is greater than 11.3. Then inside the if statement, have it set the Player’s position on the x to the left side of the screen (-11.3) and keep the y-axis position the same as the current y. Next, make the else if statement, so it checks if the x position is less than -11.3, and inside the else if statement, have it set the Player’s x position to the right side of the screen (11.3) and keep the y-axis position the same as in the if statement.

Code to Wrap Horizontal Movement

And now the Player will wrap from one side of the screen to the other. Now that the movement code is working the way we want, time to put it in its own method. That way, we keep the void Update clean for further additions.

Wrapping Horizontal Movement

Copy all the movement code, create a new method named “Calculate Movement”, and then paste the movement code into the new method. Then make sure to call Calculate Movement from void Update.

Movement Code in Function

With the movement code isolated in the CalculateMovement method, the movement in the Player script is complete.

Unity Developer, Software Engineer, Game Designer