Keeping your Player in play

Once your player is moving around in your 2D game, you’ll want to make sure the player is always viewable to the user. One way to do this is to limit where the player can travel.

Objective: Keep the player within the viewable screen and do not allow player to go above the center (to give room for future enemies).

Unbounded player movement

Starting Point: Player moves with arrow or WASD keys, but can move anywhere on and off the screen.

Steps:

  1. Set the screen bounds
  2. Compare player position to vertical bounds
  3. Set player position if vertical bounds exceeded
  4. Check if player exceeds horizontal bounds
  5. Set player position if horizontal bounds exceeded
  6. Bonus! Wrap horizontal movement

Set the screen bounds

Before you can tell the player where it can’t go, you need to define the edges of the screen.

Setting variables to store the position value at each screen boundary

To do this, create variables to store the top, bottom, left and right boundary values. These will be the max and min X and Y position values for the player.

To find these values, move the player to each edge of the screen and note the Transform.Position values in the editor at each edge. For the max Y value, move the player to the middle of the screen and use that position. I recommend using the SerializeField attribute to easily see and adjust the values in the Inspector.

Compare player position to vertical bounds

With the the screen boundaries set in code, next you’ll need to check if the player position exceeds one of the vertical boundaries. To do this, you can use an If Statement.

If statement to check player position against vertical boundaries

By using an If Statement, you can run a certain block of code only when a certain set of conditions is true. This will allow us to only limit the player movement when the player is exceeding the screen boundaries.

Note: Place this conditional logic in the Update() method after the player movement has been calculated (ie. Movement(); ), so that the player movement can be corrected in the same frame.

Set player position if vertical bounds exceeded

So, now that you can determine whether or not the player moves beyond the vertical boundaries, you can keep the player on screen by updating the Y position value to match the boundary value.

Setting the player position with the boundary value

To do this, update the player position with a new Vector3 using the current position values for X and Z, and using the exceeded boundary for Y, depending on which condition is satisfied.

Player can move up and down, but cannot move off-screen or above center

Check if player exceeds horizontal bounds

In the same way as above, you can check if player exceeding the X boundaries with another If Statement:

If statement to check player position against horizontal boundaries

Set player position if horizontal bounds exceeded

Once you know the player moved passed a horizontal edge, you can reset the player position to the boundary value for that edge, like you did above for the vertical boundaries:

Setting the player position with the boundary value

When creating the new Vector3, use the current Y and Z position values and set the X value to the horizontal boundary value.

Player can move left and right, but cannot move off-screen

Bonus! Wrap horizontal movement

If you’re looking to add that classic Pac-Man screen-wrapping feel to your game, you can implement that here as well. When checking if the player position exceeds the horizontal boundaries, you can set the new player position to the opposite boundary value.

Setting the player position with the opposite boundary value

For example, if the player’s X position is greater than the max X boundary value, set the new player position using the min X boundary value.

Player movement wraps along the X axis to keep the player on screen