Mastering 2D Movement Constraints in Unity

Brian Branch
4 min readJan 1, 2025

--

When developing 2D games in Unity, controlling player movement is essential for a polished and enjoyable experience. As I revisited my earlier articles and courses with GameDevHQ, I realized there were key movement techniques I hadn’t shared — let’s fix that..

In this case I’m adding how to create constraints for the players movement from my original article on Simple Player Movement.

The goal was to keep the player from going offscreen below and to keep them from going further than about halfway in the up direction. I also wanted to have the player wrap to the other side when going left or right off screen. Restricting the player’s vertical movement ensures they stay within the play area, and preventing them from going off-screen.

Image of Player primitive cube going off screen in different areas.

This is definitely not what I want the player to do. I’ll start with restraining movement on the y-axis. What I need is to check if the player is above or below a certain amount and then stop them from going further. I can use if-then-else statements to do this. I’ve checked the Y position where I want to set the limit which will be between -4 and 1, so I’ll write out the if-then-else statements like below.

If-then statements to constrain the player movement on the y-axis.

So I check if the player is above or below these values and then set the transform.position to those values using a new Vector3. And now the player is prevented from going off-screen below and restrains them to about half of the screen going up. You can see this in action below.

Player cannot go below screen or past halfway moving up.

The next order of business is to have the player wrap around when going off screen to the right or left. having the player wrap from side-to-side is very common in space shooter games. It allows the player to appear on the opposite side when they go off-screen.

It will be similar to how I did the restraining for up-down movement but this time I’ll want the player to change to the other side when going off screen to the left or right. I determines that on the left it will be about -11.3 and to the right it will be about 11.3. I used the below code to accomplish this.

Code to wrap the player when going off screen to the left or the right.

Now as shown below the player is wrapping when going off screen to the left or the right.

Player wrapping when going off screen to the left or the right.

Going back to the vertical restraining, there is a different way that I could do this. Unity has as function called Mathf.Clamp, what this does is clamp a result between two chosen values. If I used this instead I could accomplish the same thing but with only a single line of code instead of a full if-then-else statement.

While either way would work, using Mathf.Clamp helps keep the code simpler and makes it easier to maintain.

The above code keeps the player’s X and Z positions while clamping the Y position between -4 and 1.

What the code above does is maintain the transform position on the x-axis, but on the y-axis the Mathf.Clamp function restrains the values from going below or above the values of -4 and 1 which are provided.

And this works just like before but with less code. Now we have the player restrained on the y-axis and wrapping on the x-axis.

Player now restrained on the y-axis and wrapping on the x-axis.

If you found this useful, keep an eye out for more as I continue updating some missed information and also providing some new insights in to C# and Unity Development. Please leave a comment if you found this helpful.

--

--

Brian Branch
Brian Branch

No responses yet