Important Notice:

As of September 15, 2016, we no longer publish new courses on this website. We encourage you to use your Digital-Tutors credentials to sign in on Pluralsight where you'll find all new creative courses, skill tests and paths, 1:1 mentoring and more.
Working with physics
See Details
Joshua Kinney


Joshua Kinney
In this tutorial, we're going to discuss physics and how they work inside of Unity. All right. So to get started here, let's go ahead and add the components that we need to actually apply physics to an object. Now, the reason that we would want to apply physics to an object is because we may have situations where we have things like falling rocks, or we have objects that we want the character to interact with on a physical level, that sort of thing. So we need to be able to figure out exactly how to apply and what to apply to make each object physical. So the first thing that we need to do is add a collider to our objects. So I have this boulder here. And there are no colliders of any sort on this. So it's not going to be able to detect collision, even if we were to apply physics to it. So let's go to Component, and then go to Physics, and then choose a collider that we want to use. Now, you could use the primitive colliders, which are very inexpensive for your machine. Or you could use the mesh collider, which is going to create a collider based on the actual mesh or the graphical mesh that you have on your object. And that can become very expensive, depending on how many vertices you actually have in that object. Now, it all depends on what you need for your specific course. We could use a box collider. But what's going to happen is whenever this object falls, it's going to basically fall like a cardboard box would and just rest. It doesn't actually roll around like a boulder should. So if I go ahead and remove that and we apply a different physical mesh, let's say something like a sphere, this would work a little bit better. However, whenever a rigid body takes over, it's going to roll around like a ball. And it's not going to really work all that great. We would notice the rock clipping through the floor as it's rolling around. And it doesn't ever really come to a rest most of the time. So let's go ahead and remove that component. And what it seems like is we're only able to use the mesh collider. Now, we could use capsule collider. But that's like an elongated ball. So we're going to run into those same issues. So mesh colliders is what's going to work best for us in this situation. Now, mesh collider has one great key factor. We can actually create collider objects inside of light 3ds Max from Maya that are basically the mesh itself, but really simplified. So we don't need all of the detail that we have in here. We can create just the basic shape and input that here in our mesh instead of box 001. All right, so now that we have that, let's keep our mesh collider on there. And now what we want to do is hit Play and see what we have. Now, you'll notice that the boulder is just floating in air. And we can jump. And we are able to collide with it. But it's not falling as it should. So what we need to do is add the rigid body component to it to actually allow that motion to happen. So let's add a component. Go to Physics. And then we're going to use Rigid Body. Now, by adding a rigid body, we're making this object dynamic. So whenever the game is played, it's going to go into its rigid body and begin to animate and simulate. So the issue that we're seeing here right off the bat is the lighting itself is not being portrayed on this. And that's because the object is not using a light probes. So we need to make sure that we turn that on, because again, this is a dynamic object in the scene. So let's go in. And let's go to our mesh renderer. And here we're going to say Use Light Probes. And immediately, we're going to notice a change in its overall lighting. All right, so now, what do these options mean, inside of a rigid body? We're just going to discuss those as we go along here. So the first one we have is mass. And basically, what is the mass of the body itself? How heavy is this? And this is really arbitrary units. It's recommended to make masses not more or less than 100 times the rigid body itself or the other rigid bodies. So what that means is this is not necessarily giving it a weight. But it's really in accordance to other rigid body objects. So if I had another rigid body object in here, and its mass was 10, it's going to be heavier than this object. So the other object is going to have more influence over this object here. We have drag. And drag is basically how much air resistance effects this object when moving from forces. Zero means no air resistance. Moving on up, you're going to get more and more resistance. We can use gravity. So that's going to be always checked here. Is Kinematic. Now, Is Kinematic is a little tricky. But we're going to explain this pretty simply. Basically, are you going to animate this object inside of Unity? If this object is something like a door, and we want rigid bodies on that door, if we want it to knock objects around, we need to make sure that it's checked as kinematic. Now, what that means is this object is not affected by gravity or forces. So if I go ahead, and I check this, and we hit Play, our rock is going to stay in place until it's interacted with by another rigid body So I'm going to go ahead and turn that off there. This is best used for doors, and things like that. The next section that we have is Interpolate and Collision Detection. And these are really critical options here, because they determine how the object is going to collide with the environment itself. So Interpolate, these are going to be options that are best if you have some jerkiness in your rigid body. So if I were to play this and it were to start jerking around in between other objects, we might want to choose one of the different interpolation methods. So we have Interpolate and Extrapolate. With collision detection, this is going to be used to prevent fast moving objects from passing through other objects without detecting collisions. So to simplify that, the game, when it's running, it checks for collisions at certain amount of frames. And if an object is moving so fast that it misses that collision check, it's just going to pass through other objects. And sometimes, we may not want to do that. So we'll have to set this up to something like continuous, to where it's a good range between the discrete, which is the slowest check-- continuous is a little bit faster-- and then continuous dynamic is the fastest check on those objects. So you want to use this on really fast moving objects. All right, so normally, we get away with none and discrete on those. And again, those are going to be the less expensive methods for your rigid body objects. Now, you have constraints. And this is going to be a restriction on the rigid body's motion. So you can freeze its positions. So this is going to stop the rigid body from moving in the world x, y, and z-axes. And you can select which ones are constrained. Same thing with your rotation. It stops the rigid body from rotating around the world axis. So if we were to create something like a rigid body on our characters, this would be great to freeze those rotations so that way, the player doesn't fall over using that capsule collider that we use there. So rigid bodies are really straightforward. We can come in and, with this applied, we can do something like a set of rocks falling over. I can duplicate this. Now, one thing that you want to be careful of with rigid bodies is you want to make sure that the rigid bodies are not basically intersecting like this. If I were to hit Play, we're going to notice some odd behaviors. So you'll notice that the rocks are completely gone there. So you want to make sure that they are separated out far enough from one another that they're not touching, and also from other colliders. So right now, this is intersecting with a couple of different colliders. So we want to make sure that that is low enough that we can get away from that. So now, if I hit Play, you'll notice that those fall. And we had a little bit of jerkiness there. We might want to change that, that interpolate or the collision detection there. So some things that you just want to play around with-- and it's always a case-by-case basis. All right, so now that we've talked about physics and how to work with those, in our next lesson, what we're going to do is we're going to get started in our scripting portion, where we're going to start creating the actual rules for our game and start creating game play in this course. So I'll see you then.
In this series of Unity tutorials we are going to learn about the core features in Unity.

We will start out by learning the Unity Interface where we'll talk about the different panels and tools available in the Unity editor. From there we'll learn how to properly export and import assets into Unity. With those assets imported, we'll discuss how to create prefabs that will help us speed up the construction process of our level.

Then we'll learn how to create and apply materials to our level prefabs. Then we'll take our textured prefabs and build a simple game environment. Once the base level has been built, we will talk about adding props and set dressing our level. From there we'll continue full speed by learning how to add lights, particles, and physics objects.

Once we're happy with our level, we'll jump into scripting in Unity. We'll learn how to create a random player spawn, a HUD, item pickups, and so much more. Finally, we'll end the course with how to publish our game to the platform of our choosing.

For an additional learning resource, download your free copy of our Key Game Development Terms Reference Guide and PDF so you can get comfortable with important game dev terminology.