Limit Player Movement in Unity
In this example we define the limits where the player can move in the game, in the image below you can see the player moving freely in the scene with any restriction:
We gonna create two different limits, the player only will be able to move between two values in the Y axis and for the horizontal movement if the player get out of the screen he gonna appear in the other side:
Let’s start with the vertical movement, for this I gonna use an if statement to check if the position in the Y axis is out of the limits and keep the same position in case the condition is true:
Ok now we check when the player reach one of the limits in the X axis we set multiply the X axis by -1 to make the player appear in the other side:
And that it’s, you have defined limits for your player movement: