In this lecture, we're going to fix our game so that the player can no longer leave the edges of our
screen.
Before we do that, though, let's jump out of play mode very quickly and just tidy up our assets folder.
So I'm going to create a couple of new folders in here.
So right click create folder and this one is just going to be called the input system.
And this is just going to tidy up our input actions and our input system.
We're also going to need a folder for our scripts.
So let's right click create folder and call this one, these scripts, and then we can drag our player
script into that folder with everything all tidied up.
Let's head into our scripts folder and open up our player script and look at how we might add these
boundaries to the edge of the screen.
Now, before we start doing any coding, we're actually going to have to learn a little bit about the
viewport and world space.
Okay.
So what is the viewport?
Well, viewport space represents a normalized position relative to our camera.
That might seem like complete word salad to a lot of you.
So let's bring in a picture of our game so we can relate it to something concrete in our game.
We're already familiar with World Space, and these are the positions of our objects in the hierarchy.
Viewport space, on the other hand, represents what the camera can see.
Specifically, it's the normalized position relative to this camera.
So down in the bottom left corner we start off at position zero zero.
And then as we go around the edges in the bottom right corner, we have position one zero.
In the top left we have position zero one.
And in the top right we have position one one.
And this is what it means by a normalized position.
And to box in our player into what the camera can see, we're going to use a method called viewport
two World Point.
This method converts the normalized position on the screen to a 3D position in world space.
So we can use this to find where the edges of the screen are relative to the player in the world.
And when we go and do this in code, we're specifically going to be interested in the bottom left and
top right corners of the screen, because this will encapsulate our entire viewport.
So let's see how to use this method in code by jumping back to our player script.
And we're going to need to vector two variables.
The first one is going to be a vector two called the minimum bounce or the min bounce.
And this is going to store that vector two for the bottom left of the viewport at zero zero.
And then we're going to need another vector two for our maximum bounce, and that is going to store
the value for the top right of that viewport.
So initialize these two variables.
Let's create a new method and we'll call this a void init bounce.
So in it is just a shorthand for initialization here we're going to need reference to our main camera
and rather than finding each time we need it, let's create a temporary variable of type camera and
call it the main camera and we'll set this equal to camera dot main.
So remember that camera dot main is the camera in the hierarchy that's tagged with that main camera
flag with the camera found, let's set our minimum bounds.
So say minimum bounds equals our main camera dot viewport to world point.
This method expects a position to be passed into it, and rather than doing a vector three like it's
asking us for, we're actually just going to pass in a vector too, because we don't really care about
that Z component.
And if we use a vector two instead, then that Z component will just automatically default to being
zero.
So let's pass in a new vector two at the coordinates of zero zero.
So that bottom left corner of our viewport.
Next, we need to follow a very similar process for our maximum bounce.
So that is going to be the first of your challenges.
I want you to set up the maximum bounce.
It's going to be very similar to how we set up the minimum bounds, but we should be using the top right
corner for our viewport.
So pause the video and give that one a go.
Okay.
Welcome back.
So hopefully that wasn't too hard.
And in fact, it's so similar to our minimum balance.
I'm just going to copy and paste and then going to rename our variable to maximum balance because that's
the one we're setting.
And rather than passing in the bottom left corner of the screen to our viewport to world point method,
I'm going to change this from zero 0 to 1 one to be our top right corner.
Next, we need to cool this in it, bounce from somewhere and we really only need to do it once for
now.
So I know we deleted it in the last lecture, but let's bring back our start method and in here we'll
call this init bounce method.
Next we need to go ahead and amend our move method to account for these boundaries that we're setting
for the player movement.
And we're going to do this again with another temporary vector, two variable called new position or
new pose and start with let's just initialize that as a brand new vector too.
This will allow us to then edit the X and the Y component of this new vector and then apply the changes
back to the transform dot position.
So let's start by clamping the X position of our player so we can say that the new dot x and to clamp
this variable, we're going to use a method from the math f class called math f clamp.
And what this will do is just restrict any values that float outside of the minimum and maximum values
that we set.
So we can see here that it needs three parameters.
It needs the value being checked and it needs the minimum and maximum values that it needs to be checked
against.
So the value being passed in is going to be our transform dot position dot x.
So the current position of our player plus the Delta X, so the amount we're going to be moving and
this needs to be kept between our minimum balance dot X and our maximum bounds dot x.
Next we need to do a similar thing but clamping our y position.
So this is incredibly similar.
So again, I'm just going to copy and paste my code, but this time we're setting our new position Y
and we're clamping our transform position y plus our delta y and we're clamping that between the minimum
bounds Y and the maximum bounds dot y.
Finally, we need to apply this new position, which now includes our Delta movement.
So at the bottom we can change our transform position to equal our new position.
And while we're here, we've got a vector three for our delta position and a vector two for our new
position.
And we're generally working with two DX vectors for now.
So we can actually go ahead and just change that to a vector two because we don't really care about
that Z component for now.
So let's go ahead and save our script and jump back over into Unity to test this all out.
To say was waiting around.
I'm just going to bump up my move speed for my player just a bit.
And now if we move around, we can see that we now get trapped along the edges of our screen so we can
no longer leave the edges.
So also just check the top and yep, that all seems to be working as well.
But notice that when we hit the edge of the screen, half of our player is still leaving the edge of
the screen.
And that's because the position of our player is being tracked from its pivot point, which by default
is in the middle of our sprite.
So we're going to need a way of accounting for that.
And the way I'm going to do it is to add some padding around the edge of the screen, similar to how
we might add padding to our UI elements.
So back over in our player script, let's create some new variables and these are all going to be serialized
fields.
So I'm going to need four in total and they're all going to be Float.
The first one is going to be called padding left.
The second is going to be called padding.
Right.
And save the typing.
I'm just going to copy this another two times.
Call the third one padding top and the last one padding bottom.
The next step is to figure out where we want to apply this padding.
There are a couple of options.
We could add it to our minimum and maximum bounds, but I'm actually going to add it to where we're
calculating our new position.
So when we're calculating our new X position and taking into account on minimum and maximum bounds,
I'm also going to go ahead and add our padding in here.
So in addition to our minimum bounds, we're going to want to add our left padding amount.
And for our maximum balance, we're going to want to subtract our padding right amount with our X position
set.
Now let's do the same for our Y position.
So again, in here, we're going to be adding our padding bottom and we're going to be subtracting our
padding top from the maximum balance.
With that set, let's save our script and jump back into the unity one more time.
Now, I've changed our move speed here in the inspector, but I'm actually going to revert that for
now and jump over into my player prefab and I need to set some values for our padding.
If we look up in the transform, the scale of our player is one, one, one and we know with zero padding
that half of our player goes off the screen.
So if we want to keep our player fully in the screen, each of these values is going to have to be 0.5
and I am going to set 0.5 to be my left and right padding for now.
But for my top and bottom padding, I'm actually going to change things up a bit because we're going
to want some extra padding at the bottom to account for the UI that we're going to add later on.
And we need to make a design decision on whether we want the player to fully explore the top of the
screen or do we want to constrict them to the lower portions?
For now, I'm going to say that I don't want my player going right the way to the top of the screen.
So I'm going to add a padding of around five.
And for the bottom padding, I'm going to choose a value of around two.
These definitely aren't set in stone, though, so let's come out of our prefab and hit play to test
this out in our game so we can now see that we're nicely being constrained on the left side of the screen
and nicely constrained on the right of the screen.
If we go down, we've probably got just enough space for our UI, so we may want to bring that up a
little bit later when we start implementing that.
And if we go up, we're getting cut off just around three quarters of the way up the screen.
Again, we may want to change that in the future and bring it down a little bit maybe to around here.
But with our player now set up and able to move around, the next step is going to be to work on our
enemies and getting those spawning into the level.
We're going to start looking at that in the next lecture.
So I'll see you there.