In the previous player movement post, we added player movement with a variable speed, but we didn’t set the boundary for the player. This means the player can go anywhere off-screen.
For the prototype, I want to create movement boundaries within our camera’s view, as well as limit how far we can travel vertically. Remember, we’re working on a prototype, so I will not be precious about it. I’m going to implement this in such a way that can be easily changed without code later during playtesting.
Defining The Boundaries
I want the top boundary to be 0 and the bottom boundary to be
-3.5f, based on my testing with different values. For the horizontal boundary, again I tested with different positions to get a feel for how much margin I want from the camera view, and decided on
To accomplish this, I use the Clamp static method of the Mathf class. This method takes float arguments for a value to be restricted, and the min and max values to compare against, and returns a float value between the min and max values.
Our player movement now has some set boundaries! As you can see, we’ve added the
SerializeField attribute as well so that we can make quick adjustments during playtesting.
Now that we have taken care of everything related to the player movement, we can move on to other aspects of our prototype.
Stay tuned for the next post! Take care.