2D Shooter: Placing Boundaries

Sabrina Windsor
4 min readMay 12, 2021

So now we got the player moving around where we want it, but we don’t want the player to being able to go everywhere. It wouldn’t make much sense for the player to be able to go off screen, so we need to create some boundaries to keep them from wandering off.

To do this, we’ll be needing to use some if/then statements to get the point we want to make to be understood by Unity. Right now, let’s determine where we want our boundaries to be. Let’s look at the player’s transform position in the inspector tab when they’re at the place of the desired boundary.

I’ll determine the x boundaries by moving the player to the right until its off screen. Since I’ve made my player start at (0,0,0) when I hit play, the left boundary should be the same number, but negative.

This is where my object is off screen at the right.

So now we know that the x-axis boundaries are at 11.24, and -11.24.

For the y axis, I’m going to have one of the boundaries be 0, so we can make sure we can’t cross into enemy territory once we add them later on. We also don’t want to lose sight of our player if enemies are shooting at us, so lets have the boundary be where the player is just above the edge of the screen.

It appears our y-axis boundaries are 0, and -3.8.

Now it’s time for some pseudo code to help describe the steps of our if/then statement.

We steps are describing that whenever the player tries to go past the 0, or -3.8 position, unity will update the position back to where we tell it to. In game, this will look like the player is going up against an invisible wall.

So if the player’s y-axis position is greater than 0, we then want Unity to make the player’s y-axis position back to 0. Its the same going the other way, except we want to use less than, as we don’t want to go below -3.8.

To call the player’s y position in code, we use transform.position.y, but we want to make sure the x coordinate stays the same wherever its currently at. To do this, we’ll need to get and set vector3 coordinates, while grabbing the current x-coordinate. It should look like this:

The full if/then statement in code should look like this:

So now, whenever the player tries going past 0 on the y-axis, unity will make the position equal y-axis 0, while keeping the current x coordinate. We’ll do the same with the other y-axis boundary, except changing greater than to less than, and combining it with what we already wrote using else if.

Now we’ll have a different set of circumstances with the x-axis boundaries, so we’ll need a separate if/then statement. Plus, I’d like to try doing a wraparound effect instead of an invisible wall, so when the player goes off screen on one side, they’ll reappear on the other.

The steps will be very similar except we’ll want to get the x coordinate when the player just disappears on the screen. For me, this was at 11.24, and -11.24. Here’s the pseudocode to describe what we need to do:

As you can see we want Unity to change the position to the opposite side when the conditions are met. We’ll be using the same format as before, and it should look like this:

And we’re done! If everything is working right, this is what we should get:

Happy coding!

--

--

Sabrina Windsor

Currently learning to code with the help of GamedevHQ in order to someday my my game ideas come to life!