Setting Boundaries for a 2D Player Character

David Hunter Thornton
4 min readAug 9, 2022

--

How do I prevent my Player Character from moving passed the edges of the screen?

Objective: Explain how to use code to set limitations on how far an object can move.

At the moment in our example game of a 2D “Shoot ’em up” the Player Character can continue to move far beyond the edges of the screen. Now that the user is in control of the movement, we need to limit that movement to the camera view.

Before we touch any actual code, let’s discuss “if”, “else”, and “else if” statements:
if = used to say IF that thing is TRUE then initiate this code
else = used to say IF that thing is FALSE then initiate this code instead
else if = used to say IF a different aspect of that thing is TRUE then initiate this code instead

The “else if” statement is typically used before “else” because you want to check things in order from top to bottom. As an example, we have “if a is greater than b”, then you can say “else if a is less than b”. In this instance you’d use “else” to mean “anything other than greater or less than”. We know the only thing left is “equal to” so technically it could be another “else if”. However, if we want to encompass all potential options, we can just say “else”.

Before we start our statements, let’s find out exactly where we want to stop the player from going.

It appears that the limits of my window should be about 8.3 and -8.3 respectively for the horizontal portion (x axis). However, for the vertical portion (y axis), I don’t want the Player Character to move any higher than the middle of the screen, so that will be 0, but I need -4.4 for the bottom.

In the above, I’ve converted some psuedo code at the top in my comments, into the actual code beneath. Everything I’ve written there was covered in my previous articles. The only thing that may not seem to be intuitive is the “transform.position.x” instead of a 0. This is so that the player can continue to move side to side as desired even if they’ve reached the top boundary.

Here I’ve done the same thing but instead using “else if” because we are still dealing with the “position.y” condition. Also, since this is a decimal, don’t forget to use the “f” behind it to signify its a float instead of an integer.

For the next part, I’m actually going to do something a little bit trickier. I want my Player Character to wrap around to the other side when they go out of bounds instead of just stopping. So for the right and left boundaries, I want it to go off screen, and appear on the other side of the screen.

I used 9.4 instead of 8.3 because that was the point at which the Player Character fully disappeared from the screen. In the below example, you can see the point at which it appears on the other side in the Scene View.

Friendly Reminder: Don’t forget to keep updating your project through GitHub/Git and saving any changes you make to a separate branch. You can check out my other articles to get a breakdown of those steps! Day 1–9

--

--