From the course: AR Development Techniques 02: Lighting and Physics

Colliders and rigidbody

From the course: AR Development Techniques 02: Lighting and Physics

Start my 1-month free trial

Colliders and rigidbody

- [Instructor] If I look at my table, I can see a couple of things like a notebook, monitor, a microphone and some other stuff. So what's keeping this notebook on the table? Or what's keeping this monitor on the table? If you think about it, there are two main things. First is the gravity acting on the object, which has a mass and second thing is that both the notebook and the table have a solid surface so the notebook doesn't just pass through the table. Things work in a similar manner inside Unity. Let me show you what I'm talking about. Create a new Unity project and make sure auto generate lightning is on. So first of all, create a plane object. Right click, 3D object, and Plane. Reset the position. And create a sphere object. 3D Object and Sphere. Let's add materials to both the objects. Right click, Create, Materials. Plane Mat. And let's make this a little dark. And let's create a material for the sphere. Let's make this red. Now if I press play, nothing happens because this object doesn't have a mass yet and no gravity is acting on it. To add a mass and gravity, we need to add a Rigidbody component to our sphere object. So click on the sphere, Add Component and add a RigidBody. Now if I press play, the sphere falls on the plane. If you look at the RigidBody component, there are two main things, mass, which is one unit and the gravity, which is checked. So if I stop this, uncheck the gravity and play again, the ball won't fall on the plane because now it just has a mass but doesn't have any weight because the gravity is zero. So let's check this again. So the RigidBody component is what gives the object a mass and enables gravity. So when we press play, the ball falls on the plane. But what exactly makes it stay on the plane and not pass through the plane? Well, it's the colliders. So if you click on the plane, you can see a Mesh Collider component. And if you click on the sphere, you can see a Sphere Collider. So because there are colliders present on both the objects, this is why the ball collider stays on the plane and not pass through it. So if I remove collider from any one of these objects, and play it again, the sphere will pass through the plane. I'll just check this back. These colliders are added to all the basic 3D objects in Unity by default. But if you import a 3D object in Unity, you have to manually add a collider. You can do that by clicking on Add Component and search for collider and add from any of these available colliders. If it's a very complex object and you want an accurate collider, then you can add a mesh collider. So if you want to see the collider clearly, then disable the Mesh Renderer and this green sphere right here is the sphere collider. And if I create a cube, right click, 3D Object, Cube and disable the mesh renderer, you can see the box collider. Let's add a RigidBody to this cube as well. Now let's rotate this plane a little bit along the z-axis and if I press play, we can see the ball rolls down the plane. And if I rotate it even more and press play, you can see both the ball and the cube rolling down the plane. So they are behaving just like a real sphere and a cube would. Now, let's see how to add a little bouncy effect to the sphere. So first, I'll set the rotation to zero. And in the Assets folder, create a physics material. Right click, Create, Physics Material and I'll name this Bouncy. Click on the Sphere and add this bouncy material right here in the Sphere Collider. And now if you press play, the sphere doesn't bounce. That is because the bounciness is set to zero. So let's make this one. Now you can see that bounces a little. Now, we have set the bounciness to one but the actual bounciness is 0.5 because it says average for this bounce combine parameter. So the actual bounciness is the average of the bounciness of the sphere and the bounciness of the plane. Bounciness of the plane is zero because we haven't added this physics material on the plane, so the bounciness is the average of zero and one, which is 0.5. So if I add the same physics material on the plane, and press play, you can see that it keeps bouncing. Now let's talk about the force component. So first, I'll just get rid of this cube. Let's make this plane a bit longer. And set the sphere's y position to 0.5 so that it's on the plane. Now, while the sphere is selected, click on Add Component and add a force component. Constant Force. And if I change the z value to two, this will apply the force in this direction. So as you saw, that was a constant force being applied to the sphere so it keeps on rolling. There's another type of force known as impulse force, which we'll talk about in the next video while creating the AR balling setup.

Contents