Prototype: Player Boundaries

Introduction

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.

Player Offscreen Movement

The Challenge

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 9f.

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.

Movement Clamp Code

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.

Player Movement Boundary

Summary

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s