Creating Player Boundaries, and Screen Wrapping!

In the last article we got our Player to move with User Input! That’s all fun and games (pun intended), until the Player moves off-screen and get’s lost in the void of off-screen space.

This is where boundaries are important! We must keep the player on screen at all times, unless there is a good game-design reason not to.

Final outcome; Y-Axis boundaries, X-Axis wrapping!

Boundaries can be created through Colliders or by code. We will be using the latter.

The first boundary we’ll create is how far up or down the Player can go. The Y axis. We’ll be using a Clamp method, where if the Player goes above or below a certain y-position, they’ll be snapped back to the boundary.

The first picture is writing all the code manually. This is helpful to understand the next part.

Here, if the Player’s transform.position goes above 5, the Player will be snapped back to 5. Conversely, if the transform.position goes below -3.9, it will snap back to -3.9 y.

We have to make sure that when creating the new Vector3 for the Player, that we use the Player’s current transform.position.x as the X value. If we put a number or variable in here, anytime the Player triggers the if statement, they will snap back to a predetermined location on the x-axis.

We can achieve the same result by using a predetermined function called Mathf.Clamp !

Mathf.Clamp needs (float value, float min, float max). The value determines what will be getting “clamped” in between the min and max values.

A little less code, but it helps down the line, and is a very useful function!

Here is the result of the Y-axis boundaries. Both methods, manual or Mathf.Clamp will achieve the same result:

Perfect! Our Player can no longer escape up or down. But we still have to take care of the X-Axis.

For this we’ll be creating a ‘wrap effect’, where if the Player goes all the way to the left of the screen, they will appear on the right side going the same direction.

This adds a little more depth to our game, making the Player not feel as trapped, and confined to 4 walls.

Here we created a variable xWrapPoint. Since the camera is centered, we can just use the negative and positive xWrapPoint as our left and right bounds.

If the Player goes beyond the -xWrapPoint (left side), they are transported to the xWrapPoint (right side), while maintaining their current transform.position.y . From right to left is also true.

This is the wrap in action:

I cleaned up the code, putting them into their own functions, and called them within Update. Now if we have problems in any section, we know exactly where to look!

Audio Engineer turned Unity Game Dev. Will be combining both my skillsets when appropriate, and will be documenting my Unity Dev growth in these Medium Articles

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store