In this lecture and in the following lecture, we'll learn how Collision Detection and Resolution work. We'll see how colliders in Physics materials help us with that in Unity. what's Collision Detection? Surprisingly, Collision Detection is detecting collisions between two game objects in our Unity scene. If we add colliders to both of the game objects that participate in a collision, the 2D engine will detect that collision. What's Collision Resolution? It's doing something based on the fact that a collision has been detected, that a collision has occurred, we can tune how the 2D Physics engine resolves collisions by adding Physics materials to the colliders for our game objects. But we can also implement a custom collision resolution in our script. We're going to go to Unity to start looking at how this works. But I'll tell you we're going to spread this across two separate lectures. We'll start exploring Collision Detection and Resolution now. Before we add Collision Detection and Resolution to our game, I want to show you some changes I made to our mover script. The big idea behind the changes I added to the start method here is that instead of having the teddy bear just move to the right, I want the teddy bear to move in a random direction with a random speed. As you know from the 2D physics video, the way we get our game object moving is we add a force to the Rigidbody2D attached to that game object. The first two lines of code I've added here are constants for a Minimum Impulse Force and a Maximum Impulse Force. We'll use these numbers as we generate the force vector we use to add the force to the Rigidbody2D. The next thing we do is we generate a random angle between 0 and 2 PI radiant. That's between 0 and 360 degrees but because the trig functions in Unity use radians for taking sine and cosine and so on. We might as well just generate an angle in radians rather than generating an angle in degrees and then converting to radians. After we execute this line of code we'll have a random angle that's between 0 and 2 PI. Now I want to generate a direction vector. In other words, in what direction will we apply our force? It's a vector2, because we know the first argument to the AddForce method is a vector2. I'm starting to work on that vector2 pass in. I'm calling the constructor, and I'm passing in an x, and I'm passing in a y. For x, I'm just taking the cosine of that random angle that I generated on line 18, and for y, I'm passing in the sign of that random angle that I generated in line 18. Now, I know that some beginning game developers aren't as comfortable with Math as more experienced game developers. I promise you, we won't have to do any calculus or any higher-level Math in the work we do. But as a game developer, you're going to have to understand some basic trig functions. Trigonometry functions cosine and sine and 18, 2 and a few trig functions. Once we've done this, we have a direction vector which happens to be a unit vector. In other words, it's a vector with magnitude 1 and the x and y components are the cosine of that angle and the sine of that angle. One more random number we want. We want to generate a random magnitude that is between MinImpulseForce and MaxImpulseForce. Now we have a random direction vector, that's a unit vector whose direction is based on the random angle we generated, and we have a random magnitude that ranges between MinImpulseForce and MaxImpulseForce. Finally, we can add the force to our Rigidbody2D. I'll get the Rigidbody2D component that's attached to the same game object the script is attached to. I'll call the AddForce method. My first argument needs to be a vector2. Direction is a vector2 and magnitude is a float, so the way this works is this yields a vector2. Where the x component of that vector2 is this times magnitude, and the y component of the vector2 is this times magnitude. If that's confusing to you, should google multiplying a vector by a scalar, and it works exactly the way I just described. Our first argument now is a random impulse force vector, and the second argument is so that we apply an impulse force rather than a force. Remember the ForceMode2D enumeration has two possible values. We'll use impulse for this. I'll run the game to show you how this works. The teddy bear starts moving in a particular direction. I'll run it again and it moves in a different direction with a slightly different speed. You may not have been able to see that. But we've got random directions and random speeds for our teddy bear. That had nothing to do with the collision detection and collision resolution, but it's a really good way to get a game object moving in a random direction with a random speed. Let's actually start adding some collision stuff. I'm going to run the game one more time and you can watch either the scene view or the game view. But you'll notice that the teddy bear just leaves our game and we'd like to have the teddy bear stay in our game world. In fact, we want the teddy bear to stay within the scene so the player can keep seeing it. Let's start by adding a collider to the teddy bear. I'll select the teddy bear and over here in the inspector, I'll add a component and I'm adding another physics 2D component. This time I'm going to add a collider. There are lots of different shapes that we can use for colliders. I'm going to add a box collider because my teddy bear is rectangular. I'm also going to move this component up a couple. Just because remember that's my personal preference to have my scripts down at the bottom. Now, if I double-click the teddy bear to give it focus, you can see that green box around the teddy bear is the collider for the teddy bear. Now because I made my width and height of my teddy bear sprite be powers of two because that's more efficient for the graphics card, we have some space on the left and right of the teddy bear, and we don't want that space to be included in our collisions. The good news is, I can come over here to my collider, and I can click this Edit Collider box. Then over here in the scene, I can grab those boxes on the collider edges and move them more tightly into the teddy bear. When I'm done, I just click this box again. Now I have a tighter collider around the teddy bear. The sprite is still 32 by 32, but the collider is smaller, so we get tighter collisions between our GameObjects when the teddy bear collides with some other GameObject. I'll run the game again and you should watch the game view. The teddy bear still leaves the screen because it doesn't collide with anything. For collision detection to work, both participants in the collision need to have colliders attached to them. Let's select the main camera. I'm going to scroll in just using the scroll wheel on my mouse. What we really want to do is we want to have colliders on each of the edges of the main camera so that the teddy bear can collide with those colliders. Just to be clear, I don't want to just add a box collider to the camera with the box being the edges, because the teddy bear would always be in collision with that box collider. The teddy bear box would be overlapping with the camera box always, so that won't work for us. We actually have to build a box around the camera with a collider on each edge. With the camera selected, I'm going to add a physics 2D component. I'm adding an edge collider 2D. There are a couple of ways I can edit this collider. One way is to click this button just like we did with the teddy bear. Now I can drag the endpoints of the edge collider to where I want them to be. That's a pretty quick way to do it. It's a little imprecise. Another way we can do this is if we expand this point section, we can actually explicitly set the end point for the collider. I happen to know with this camera's size that the Xs are at negative 5.3333 repeating and the Ys are actually at 3. Negative 3 and positive 3. Just to show you, I still have my collider in the appropriate place. Two different ways to edit the edge collider. You can use the button like we used for the teddy bear collider or you can set the points explicitly. I'm going to go add three more edge colliders to my main camera and then we'll come back. As you can see, I now have four edge colliders, one at each edge of my camera view. By the way, over here on the right, I can collapse the information about different components by clicking the arrow to the left of the component. You can use that technique to clean up the inspector so you can focus on the component you're working with. Now the teddy bear has a collider and the camera has a collider on each edge. This should work great. Let's go ahead and run the game. Well, that doesn't work as great as we would like. Although the teddy bear is in fact banging into the edge collider, it's not bouncing off that edge collider the way I'd like it to, you may want it to actually run into the edge and glide along it as you saw that teddy bear doing, but I don't want that. I want the teddy bear to actually bounce off the edges. As I said, I've broken this topic into two separate lectures. This is the end of the first lecture.