This is the second lecture about Collision Detection and Resolution. Let's go continue our unity exploration of those topics. I want the teddy bear to actually bounce off the edges. We can fix this problem by adding something called Physics 2D materials. I'm going to add another folder in my assets folder and I'll call this one Materials. I'll create a new 2D, Physics Material 2D. I'm going to call this one EdgeMaterial. As you can see over on the right, this Edge Material has, by default, a little bit of friction and no bounciness. That's not at all what I want. I want us to have no friction and lots of bounciness. While I'm at it, I'm going to create a teddy bear material as well, so create 2D, Physics Material 2D and I'll call this one TeddyBearMaterial. I'll come over here and I'll make the teddy bear frictionless and completely bouncy as well. I know teddy bears aren't in practice bouncy in general, but I want my teddy bears to be bouncy. Now you might wonder why I defined two materials if they have the same characteristics. The reason I did that is because as you tune the game, you might decide that you want to change the teddy bear's characteristics of friction and bounciness, but leave the edges frictionless and bouncy, so this gives us the ability to fine tune our physics 2D materials. We still haven't actually included them in our game yet. To do that, let's take a look at the teddy bear. I selected the teddy bear. If we look at the box collider, you can see that the material for the box collider is set to none. There's no physics material associated with the collider for the teddy bear. One way we can add the material is I can come over here and just drag it over and drop it onto the box collider. It's been a while since I saved, so I'll save. For the main camera, we need to add materials to each of the edge colliders and we could drag and drop like I just showed you or I can just click this circle with the dot to the right and it lists all the materials that I have defined for my project, so I can just pick the edge material like that. Whatever your preferences for doing that, you can do that. I'll set the materials for the rest of the edge colliders and then we'll come back. Now that I have those physics materials on all the colliders in my game, when I run the game again, you'll see that the teddy bear bounces around the game world. This is the physics engine doing both collision detection and collision resolution. I'm going to stop that. I know that it can be hypnotic, just watching the teddy bear bounce around the scene. The physics engine detects the collision between the teddy bear collider and whatever Edge collider the teddy bear is colliding with and then it resolves those collisions by using the physics materials to change the velocity vector of the teddy bear. It's not actually changing the speed of the teddy bear, the teddy bear just keeps moving at the same speed, it's changing its velocity vector, changing the direction in which the teddy bear is moving without changing its speed. You can think of velocity as an x component and a y component of a velocity vector or you can think of velocity as a unit direction vector and a speed, either way is fine, but no matter how you think about it, the speed stays the same and the direction changes when the physics engine does collision resolution for those collisions. This is great. We get lots of collision detection and collision resolution for free, not quite free, we had to do a little bit of work, but the engine is doing all the hard math underneath to make sure this works properly. We may want some custom behavior for a collision resolution as well, we may want to make the teddy bear lose health when it bounces against these Edge colliders, we may want to gain points when the teddy bear bounces against the edge collider, so that would be a great way to just boost your score, just sit there and your score will go up and up, but let's add some custom collision resolution to our teddy bear. I'm going to rename my Mover script TeddyBear because it's now going to capture the behavior of the teddy bear both when the teddy bear gets added to the scene, when the Start method gets called so it starts moving in a random direction at a random speed, and for how it resolves collisions. I'll select the teddy bear and I'll close down some of those component. I'm going to deactivate the talker because we don't care that it does the talking stuff anymore. But you will notice that for the teddy bear script, it says it can't be loaded because there are some compile errors. Let's go see what's causing that problem. When we come over to Visual Studio, we have this error showing up in this window because we had the mover script opened up, and that mover script no longer exists. It's now called TeddyBear.cs not mover.cs. I'll just close that window. Over here in the Solution Explorer, I will expand and open the TeddyBear script. Here's what's causing that compilation error. We need our.cs file to be the same name as the class. I talked about this previously as a mistake. You might have it called new behavior script here, and you have to change it because you renamed the script in the editor. This is that same exact problem, but we can easily fix that problem by changing the name of the class to TeddyBear over here. I'll build and you can see back in the editor that that error has gone away, everything is fine. The next question though is, how do we add collision resolution behavior in our TeddyBear script? Well, it turns out that all of the scripts that we create including the TeddyBear, do something called inherit from a class called MonoBehaviour. We'll learn about inheritance in a few courses, but the big idea is any method that appears in MonoBehaviour, we can include in our TeddyBear script. At this point we should go read the documentation for MonoBehaviour. Back in the editor, I'll select Help, Scripting Reference, and I'll search on MonoBehaviour. Notice that's with a U, and I could just click the class name, but I actually can see right here, MonoBehaviour.OnCollisionEnter2D is sent, which means it's called when an incoming collider makes contact with this objects collider. That's exactly what we need. Here's a description of how it works. Remember, the physics 2D Engine is detecting collisions and it's resolving collisions by doing the bouncing that we got set up, but it will also call an OnCollisionEnter2D method in our script if we include one. So we're going to include one. The easiest way to make sure we do this right, is I'll just scroll down and we'll grab this example of including the OnCollisionEnter2D method, and I'll scroll down even though the order of the methods don't matter here in this script, I like the start methods to come first, and I'm going to replace this line comment with a documentation comment called on a collision. The parameter actually provides us some collision information. We won't use it in this particular example, but we could use it if we wanted to and we'll actually print a message as well using debug log. But I'm not going to say OnCollisionEnter2D, I'm going to say ouch, because the teddy bear's getting hurt. We don't have any health for the teddy bear right now, but it hurts the teddy bear to run into the edge of the screen. I'll build, and build succeeded. Now when I run my game, each time the teddy bear collides with an edge collider, it prints the ouch message over in the console, and the physics 2D engine keeps working just fine. It keeps doing the collision resolution of bouncing the teddy bear properly off the edge collider as well. But it's also calling our OnCollisionEnter2D method so that we can print that message. That's how we can add collision detection and collision resolution to our Unity game. To recap, in this lecture, and in the previous lecture, you learned the big ideas behind collision detection and collision resolution. You learned that we can use 2D colliders so that to the Unity 2D Physics Engine will detect collisions. We saw how we could use 2D physics materials to affect how the 2D physics engine resolves those collisions from a physics perspective, and you also saw how we can implement custom collision resolution in our scripts.