Okay, the title of this lecture is also the title of a song by one of my favorite groups of all time. So if you've never checked out a group called The Clash, you should go do that now and then you came back to this lecture video. Once you do, within this lecture we're going to learn how to clamp the game object so that it stays within the screen. And the word clamp is used when we talk about keeping a particular value within a range of values or a specified set of values. So we clamp a value to stay valid, basically. Okay, let's go to Unity and see how we can do that to keep our character inside the screen. In the previous lecture, we got our game set up so that the character follows the mouse around but the character can leave the game window. So we want to take care of that now. I've done some preliminary work setting that up. I've added a couple of additional scripts. I've added a ScreenUtils script and a GameInitializer script, and the Main Camera now has the GameInitializer script attached to it. So let's look at those new scripts and the changes I've made to the Character script. And then we can get to work keeping that character clamped in the game window. So the ScreenUtils script is a static class that I wrote. Notice it doesn't inherit from mono behavior here on the end, and it's static. So when consumers of this class access the properties that this class provides, they'll start by putting ScreenUtils, and we'll see that soon. So basically what I'm trying to do here is to make it so that characters don't have to calculate the locations of the screen edges every frame of the game. Instead I want to do that calculation once here and save it. And then have the consumers of the class, the characters, just access the properties here. So I'm going to save screenLeft, screenRight, screenTop and screenBottom. And then I'm going to expose public static properties for those four different edges. And here, I have an Initialize method that gets called at the beginning of the game. And it's going to save the screen edges in world coordinates, and there's a bunch of math here that I do to do that. It's pretty straightforward, but you can walk through that on your own if you want to understand how that math works. Now ScreenUtils wasn't attached to the Main Camera, the Game Initializer script was. And all the Game Initializer script does is it calls the ScreenUtils.Initialize method. I'm actually doing this from within an Awake method, which gets called before any of the start methods get called. And when we need to worry about that, we'll talk about that some more. But basically this initializes the ScreenUtils so that consumers of the class can access these four properties. And the consumers of the class in this case will be the Character script. Now, I've added some stuff to the Character script as well. I've added saving half the collider's width and half the collider's height. Because if you think about this, for example, the way we can determine if the character is outside the left-hand side of the screen. Is that if we take the location of the character and we subtract half the collider width, so that's the left edge of the character. If that left edge of the character is less than the left edge of the screen, then we've in fact exited the screen a little bit. And we need to bring the character back into the window or the screen, I'll use those terms interchangeably, to clamp the character to stay inside the window. So I told you in the previous lecture that I knew I'd need the Start method and here's where I need it. So I'm going to retrieve the BoxCollider2D, that is the component that's attached to the character. And I'm going to save it into this local variable, and then I'm going to calculate the colliderHalfWidth as the horizontal, or width, size of that collider divided by 2. And I do the same thing for colliderHalfHeight. And this saves me from dividing by 2 to get half the width and dividing by 2 to get half the height every frame of the game. Down here in the Update method, we still follow the mouse around with the character like we did in the previous lecture. But we're also going to call a ClampInScreen method that I wrote that doesn't do anything right now, so I'm going to add a comment, position as necessary, And the first I'm going to do is save the current position into this position variable. So now I'll check to see if the position- colliderHalfWidth < ScreenUtils.ScreenLeft. If that's the case, then my character is at least partially off the left-hand side of the screen. If that's the case, I will set the x location equal to the left side of the screen, + colliderHalfWidth. And that will make it so the character is pushed right up against the left-hand side of the screen. Now I'll do an else if to check if I'm partially outside the right side of the screen and I'll show you that once we come back, I will point out that this is an else if rather than an if under the assumption that the character is not wider that the screen. You've got bigger problems if the character is wider than the screen. So I can assume we're either outside a the little bit on the left, or outside a little bit on the right, but never both. So in this case I'm checking to see if position + colliderHalfWidth, so it's the right side of the character. If that's greater than the right side of the screen then I'm sticking a little bit outside the right side of the screen. And so I set the position to ScreenRight- colliderHalfWidth, and that puts my character right up against the right-hand side of the screen. I'll add the logic for top and bottom, and then we'll come back. Okay, so here's the code for top and bottom. And you might find that you need to draw some pictures on a piece of paper with a pencil, and see that you understand why this all works. So do that on your own. So at this point, we've clamped the position so that the character is totally inside the screen. But we haven't actually changed the transform position attached to the game object. So our final step is to set transform.position = the position that we just calculated here. Now we might not have made any changes to position at all, if we didn't need to clamp. It's the same thing. But we might have changed x or y or if we were in a corner, both. So this makes sure that the game object transform position is set to that clamped position. And, of course, when I go to compile, I get errors all over the place because I really needed to be checking postition.x, position.x for the horizontal clamping, position.y, and position.y for the vertical clamping. Now I actually get a build successful so I can go back to the editor. And now when I run my game, we can see the character is clamped on the left, right, top, and bottom. So we've implemented the clampdown so that the character stays completely inside our screen as the player plays the game. Before we finish the lecture, you should do an in-video quiz showing your understanding of clamping. To recap, today we learned how we can clamp a game object's position to keep it inside the screen.