In this lecture, we'll learn how to add some physics to our game. Now Unity actually has two different physics engines, a 2D physics engine and a 3D physics engine. And we will exclusively use the 2D physics engine in this course. So you should go do an in-video quiz to tell me that you understand why we're doing that. And now we can go to Unity and start adding some physics to our game. All right, let's add some physics to our teddy bear. As you can see, I already have the magnifier turned on. And I'm actually going to go to the main camera. And remove the talker script from the main camera. I don't want that attached there anymore. The teddy bear will be the only thing that talks. So if you go all the way to the right, near this gear. And click, down, you can actually select Remove Component. So if you decided you no longer want the component attached to a particular game object, you can just remove it. So now if we play the game. Over in the console window, we only get one message and that is from the TeddyBear. So i'll turn off the game. To add Physics to our TeddyBear, we'll select it here in the hierarchy window, and we're going to add a component to it. And the component that I'm going to add is a Physics 2D component, specifically a rigid body 2D. We're going to deal with rigid body physics in our games, so I've added a rigid body to my teddy bear game object. I will also say that I like to over here in the inspector, I like to keep my scripts on the very bottom. That's just personal preference. You can have the components in whatever order you want but I'm going to move that script down by clicking the gear, and saying move down. And that way my talker script is my very last component. Now when I run the game because my teddy bear has a rigid body attached to it, it will do physics things. In case you missed that, let me run the game again and swoop down to the game view. The teddy bear just falls right out of the scene. And that's because there's this thing that we get by default in physics and it's called gravity. So let's actually for this particular game we're working on, let's actually turn off gravity. The way we do that is on the top menu bar, we go to Edit > Project Settings down near the bottom, Physics 2D. And over where the inspector usually is, we see our Physics 2D settings. And the very first item is gravity where there's no gravitational force in the x direction horizontally but there is a gravitational force and that number should look familiar if you've ever done physics. There is a gravitational force pulling down in y. So to turn that off, we just change gravitational force in y to 0 as well. I will Ctrl+S to save. And now I'm going to run the game again. And you see that the teddy bear just sits there. Because there's no gravitational force or any other force applied to the teddy bear. Let's apply a force to the teddy bear to get it moving and I'm going to add another script to my game. So we already have a talker script, and I'm going to add a mover script, something that pushes a game object. And so I will call this Mover, and I will double click it to open it up. And I will add a comment that says, moves the game object. And I'll get rid of update again because we don't need it. In case you think we never, ever need update, we will need update, we just haven't needed to use it yet. And so here, I'm going to make a comment that says, get the game object moving. Now, just as in normal physics, we get things moving by applying a force. And to understand how to apply a force, we need to understand that it's the rigid body 2D component that we need to apply the force to. And we don't know inherently how to actually do that. So this is a good time to go look at the scripting reference for Unity. Back in the editor, if we go to Help on the main menu bar, and we say 'scripting reference, it will open up your default browser at the Unity scripting reference. I'm going to search here on the upper right on RigidBody2D' And I'm going to pick the very first hit in those search results. As you can see, there are lots of variables or fields or properties. You can think of those however you want to. And there are also public functions. And these are really methods, think of it that way, in C#. Other languages call them functions, but C# calls them methods. But it's essentially the same thing when you're reading the documentation. And look at this very first method, add force. So this applies a force to the rigid body. Let's click that and we can see that it returns void, and here's the method name, Add Force, and we provide a vector 2 for the force we want to apply, and we also apply a force mode 2D, which we'll look at in a moment. So we'll hand in two arguments to this method. Now it turns out that this ForceMode2D mode = ForceMode2D.Force means that, that second argument is actually optional and if we don't provide it, it will default to this force over here at the very end. We'll just explicitly provide it when we call the AddForce method. So the parameters are the force, the components of the force in the X and Y axes, and the mode, the method used to apply the specific force. And to find out our choices, we'll click this ForceMode2D link. And this brings us to something called an enumeration that only has two possible values, force. Applying the force using its mass, an impulse which is an impulse force that we apply instead. And we're going to actually apply impulse forces to our Rigidbody2Ds, but looking at the documentation we now have sufficient information, to do what we need to do in our script. So the first thing we need to actually do, is get a reference to the rigid body 2D component. So remember that scripts are components, and the scripts that we attach as a component to a game object is actually an object, it's an instance of the script class. In the same way, the rigid body 2D component that we have attached to our teddy bear is in fact an instance of the rigid body 2D class and we can actually get a reference to that object by doing this. We'll declare a variable of type Rigidbody2D and I'll just call it rb2d. So, the next piece here, is that from within this script, we can get other components that are attached to the same game object we're attached to. And we do that by calling GetComponent. And this is a slightly different syntax for a method call because we're going to add a less than sign. We're going to say what kind of component we want to get. And then a greater than sign, but because we're calling a method we still need the parentheses, and then a semicolon. This is actually called a generic method because here between the less than and greater than signs, we provide the data type of the component we're trying to get. And then we'll get that component and it will put it into this variable. Now we can add that force that we read about in the documentation. So rb2d. Remember we call methods by putting the object name, then dot. And AddForce was the first one we wanted to add. And we need to provide a vector2 for the force. And we don't happen to have a vector2 object hanging around right now for the force we want to apply, but that's okay. We can just create a vector2 object on the fly and I will apply a force of 1, 0. So this is a force to the right, horizontally And I'm going to provide the ForceMode2D argument. And I want to apply an impulse force. So I will F8 to make sure it builds fine. When we go back to the editor and run the game, and our teddy bear doesn't move at all because we need to add the script as a component to the game object for it to affect the behavior of that game object. And now I've selected the teddy bear object, and you can see that the mover script is also attached. So we can attach multiple scripts as components to a particular game object to affect its behavior. And when I run the game, the teddy bear starts moving to the right and he talked. Now I will say that if you didn't want to use this variable right here, you don't actually have to do it that way. So I'll comment out this code above and toggle line comment. And then I will just say get the rigid body component and now that you're holding that object, add a force to it. And I'll move this down just to make it look nice. So we can call this method which returns an object and now that we're holding an object, we can call methods on that object. So there are two different ways we can do essentially the same thing. And I'll show that it still works by running the game one more time. And you see our teddy bear moving. And so we've added some physics to our teddy bear. To recap, today we learned how to add physics to our game by using the Physics 2D Engine. And that's a huge win for us because we don't actually have to code the physics simulation, the Unity engine does that for us. We also learned that we can access things like the rigid body 2D component from within a script, so that we can actually use physics functionality like adding force to a game object through a script, and that's great as well.