[MUSIC] In this video, we’ll discuss physics. Physics is an important part of many games. Unity actually has two physics engines, one for 3D physics, based on the PhysX engine, and one for 2D physics, based on the Box2D Physics Engine. In Roller Madness, we'll be using just the 3D physics, since this is a 3D game. Our objectives are to understand and modify colliders, understand and apply RigidBodies, and make use of physics materials. Okay, I'm gonna go ahead and click on the Roller Ball game object. And notice the roller ball has a sphere collider on it. It was part of the original prefab that we got from the standard assets. If I click on the floor, notice the floor has a box collider on it. And if I actually look at these in the scene, you can actually see the colliders. Let me go back to the ball and click F. This green circle is basically representative of the collider on this object. If I go over to one of these bumpers, it has a box collider and you can see the green box. So the collider component is what is actually used to detect collisions on the game object. You can actually modify the size of that, It could be bigger or smaller, than the actual size of the game object. For example, if I scale this up to let's say five, you can see that that green box got bigger than the actual mesh of the game object. So let me go back to four. Of course I could just change it here, or I could click revert to prefab and it will go back to the options of the prefab. So by default Unity physics does not allow colliders to overlap. So you know when my ball is rolling and it hits the bumper, it prevents it from going through it. One of the options of a collider is Is Trigger option. Let me go ahead and select this and play. And then roll over to that. And you can see whoa, I'm going right through this game object now. So by default colliders prevent things from overlapping but if you turn on this Is Trigger it makes this into a trigger. So triggers are useful to detect a collision between game objects without actually preventing the object from passing through each other. So for example the bumper could be an invisible object that when entered causes something to happen in the game, sort of like a pressure plate or that sort of thing. Right now you can actually see it, but if you turned off the Mesh Renderer, now it's an invisible object that the ball can roll into. When it hits the collider, an action could be triggered for example. We'll see examples of that as we continue. So let me go ahead and turn that back on or I could revert to the prefab once again. Let me go to the floor and let's set the is trigger for that and then play and notice what happens. Let me go ahead and change my scene views so I'm focused on the ball, go ahead and press play. And whoa, what happened? The ball fell right through the world, because once again since we set the floor collider to be trigger rather than the standard. It didn't stop the collision from happening. So one thing that you can ask yourself is why did the ball fall through the floor but not the bumpers? Let's go ahead and look at the ball and notice the ball also has a component called the rigid body. RigidBodies allow colliders to be affected by physics. So since the ball has a RigidBody component, and notice one of the options of the RigidBody component is Use Gravity. So that's basically, with the Use Gravity on, that applies a force constantly to the ball that pulls it down. If I look at one of the bumpers, there is no RigidBody component on it. Therefore, it doesn't fall with gravity. It's generally a good idea to have rigid bodies on any game objects that have colliders and that are moving in some fashion, like the Roller Ball. So we looked at the Use Gravity setting on the Roller Ball. One other option of the RigidBody is Is Kinematic. If Is Kinematic is checked, the object will not be driven by physics. This is useful when you want to move an object directly through the transform. So with RigidBodies you typically don't move the object. I mean, you can set it up through the transform but once the game starts playing you move it by applying forces, like gravity, to the ball. And the ball script actually moves the ball by applying forces. That's how we get the more natural rolling feeling rather than just moving it directly through the transform. But if you wanted to modify the transform directly through scripts, or if you wanted to have an animation on the ball through the animation system which we'll talk about a little bit later, you would turn Is Kinematic on. But in the case of the Roller Ball, we want it to be affected by physics directly, we want it to have gravity, so we'll set it this way. The other options here, you can set the Mass, the Drag, and the Angular Drag for the game object. These are things that you can always play with and kind of see the results of, by changing these options it will change how physics is affected on these options. So let me go back to the floor and of course turn the Is Trigger off, so the ball doesn't fall through the world. So let me go back to the Roller Ball now and let's look at the Sphere Collider. One option of colliders that's kind of interesting is called the Material. Now this is very different than the material that has the shader and the textures that we've talked about before. This is really a physics material, and notice it's got a Rubber option here. Essentially, the ball sort of behaves like a rubber ball in this world when physics applied to it. So let me raise the ball up a little bit and just go ahead and hit Play. And you can see it fell and it had a little bounce to it. Let me go ahead and hit the target here and let's just set it to none and play again. No bounce. Okay so that Material on the collider, let me go back and switch it back to Rubber, is what makes this have that little bounce. So you can see there's several physics materials. There's Bouncy, Ice, max friction metal, wood, zero friction, of course, rubber. You can actually make your own physics materials under Create. You can create Physics Materials right here, and set the option of bounciness and a few other options. So we have these bumpers in the world and right now they're really not very bouncy. When you hit the bumper you'd expect to be bounced off. So let's use the physics material for that purpose. So let me go to bumper, notice the Box Collider does not have a Physic Material attached. I'll go ahead and hit Target and select Bouncy. If I hit F, you can see which bumper this is. So if I play, now when I roll over to it, boom, it bounces off. If I hit one of the other bumpers, it has a little bit of a bounce because the ball is rubber, but not nearly as much as that other bumper that we had applied that physic material. So we like that we want that. So let's go ahead and hit Apply to the pre-fab on that game object. And notice all of the other bumpers now have that bouncy physics material applied. As a quick review physics are created through two components on game objects, the RigidBody and the collider. The RigidBody component enables physics on the game object, including gravity. Through the RigidBody, you can specify the physical properties of the game object, such as mass. If a collider component is also attached, the RigidBody will also detect collisions between game objects. While a collider is invisible to the camera, it determines the shape of the game object in terms of how it interacts with physics. The RigidBody and collider work together to detect collisions between game objects. If Is Trigger is set to true objects can pass through each other but the game can still detect the collision and respond accordingly. Colliders can have a physics material attached to modify it's physical properties such as make the game object bouncy. In the next video, we'll modify the player to control and look better. [MUSIC]