Creating Bounds so that the Player stays within the screen

Gabriel Perez
4 min readMar 25, 2021

--

Let's trap the Player in. Day 9

Trapping the Player within the screen is just so wrong but a necessary evil for the type of game I’m currently prototyping.

So what do I have to do to create Player bounds? Whenever the Player reaches a certain value on the x and y-axis, we want the Players position to stay within the screen vertically and wrap around the screen horizontally.

I grabbed the x and y transform values where the Player will bound on the vertical edges and also where the Player will wrap around on the horizontal edges by moving the Player object on the Scene view and recording its position.

I am moving the Player game object in the Scene view to the edge of the Camera view box. This is how I can see its current position in the inspector.
While moving the player object on the Scene view, we can record the value of its position by looking at the object's transform component. In this case the y position. We can do the same for the x position.

Here’s the pseudocode for bounding the player from disappearing from the screen.

I need to create conditions through the use of the “if” and “else if” statements. I also need to use the Player’s transform component to access its position on the x and y-axis.

Here’s the code for bounding the Player Vertically, and Wrapping the Player horizontally.

For the bounding of the Player’s y position, the top of the screen, We need to check to see if the Player is greater or equal to the vertical value of 6.5. The value I recorded earlier. If it is, then we update the Player’s current position by creating a new Vector3 with the argument x having the current position x of the Player, argument y having the value of 0, and the argument z having also the value 0.

We have to do the same thing at the bottom of the screen. The only difference is that we have to check to see if the current Player’s y position is less or equal to the vertical value of negative -6.5.

Vertical bounds… ohhhhh yeah!

For wrapping the player around horizontally and making it seem seamless, I recorded the value of the Player’s x position when exiting the screen to the right.

While moving the player object on the Scene view to the right, we can record the value of its x position.

We then need to check and see if the Player’s x position is greater or equal to the horizontal value of 13f, then we update the Players current position by creating a new Vector3 but remember, I want the player to wrap around the other side so we have to make sure the argument of x is negative -13f. This way, it spawns on the opposite side.

We then have to check for the other side which is almost similar but instead, we check to see if the Player’s x position is less or equal to the horizontal value of 13f, if so, then we change the argument x on the new Vector3 a positive 13f, argument y equals to the Player’s current y position, and argument z equals to 0.

This will give us the illusion that the player is wrapping around the screen. But instead, it’s spawning to the opposite side.

Such trickery!

Now to refactor the code for a cleaner Player script. I separated the code for bounding the Player vertically and wrapping the player horizontally into their own methods. I have also created constant variables to replace the hard-coded values since I know that they won’t need to be changed.

Cleaning up and refactoring makes me happy!

Now I will commit the changes to my GitHub, and continue learning!

I am currently committing to the development branch. I then release to the main branch.

Thank you for reading!

--

--

Gabriel Perez

Hello everyone, My name is Gabriel Perez, I am a Unity Developer and a creator who is always learning and experimenting.