Screen Bounds

Before we can set our boundaries, we have to know where we want them to be. We can do this by moving our player object to the upper, lower, leftmost, and rightmost sides of the screen and taking the coordinates from the inspector.

Then decide if you want your player to bound into the middle of the screen, or be able to wrap/warp around the edges, Pac-Man style.

Let’s pause and talk about if,then statements. This is what we’ll need for boundaries.

The pseudocode could looke like this.

And the syntax, like this:

And now, an example:

We h ave two variables with assigned values:

So, if myTemp is greater than or equal to _feverDream, then the code runs and I get the message “You’re in a fever dream, kiddo.”

So now let’s look at our y-boundary code:

Here’s what it’s saying:

“If the player’s position on the y-axis is greater than or equal to six, then stop it at 6, but let it go anywhere on the x-axis.”

transform = player

position = player’s position

y = player’s position on the y-axis

>= greater than or equal to

6 — max y value allowed

Here it is in action:

And now for the bottom boundary, add an else if statement.

And here we go:

Now let’s make the x-axis bounds and add the screenwrap function.

First thing, if we’re no longer using the y-axis, we must start a new if statement.

And that’s it!



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
Cliff Bailey

Cliff Bailey

Software Developer w/ADHD learning and doing life.