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.
Creating the colliders
See Details
Joshua Kinney


Joshua Kinney
In this lesson, we're going to build the collisions for our character. All right. So in the last lesson, we had brought in our character, and we had started to parent everything together. We've got everything set up. We're ready for animations. But before we get into that, I want to set up our colliders. So that way, whenever we're ready to get the animations in, we can just start with movement of our character. So to get started here, let's go ahead and select the main character object. And I'm going to go ahead and just lift him up just a little bit, so we can see him a little bit better. And actually, let me go ahead and just couple my Game tab up here. Just make that a little bit bigger. We don't need that for right now. We've already set up our camera. We're ready to go. So, this character doesn't have any colliders whatsoever on it. So we're going to add this as a component. And we're going to go to Physics and 2D. And we've already seen this before. We've done it with the rocks. We used a polygon collider. But we haven't used any of the other simpler colliders. Now, the box collider is going to be very efficient. It's four vertices, so we can use this over and over again and it's not going to have that much of a tax on our memory. So let's go ahead and use a box collider. And you can see that it comes in here. It's kind of hard to see. Let me go ahead and pull this over here. You can see that it comes in, but it's not around our character. So let's go ahead and make some adjustments here. Let's move the center. And that's going to basically offset that collider. We can adjust the size, and we can adjust in the y as well. And then if we just keep adjusting the center here, we can get a better collision. Let me go ahead and move that up. Let me go ahead and stretch that out along the y. And it's going to go right over the top of the head here. Now, anything that's inside of this area-- so let's say that we have an enemy, or projectile. And it comes in, it's going to hit this collider right here. And then it's going to create an effect. So let's say that an explosion or something like that happens. That's going to be kind of annoying if we jump and it keeps hitting this collider that's way out here. So what we want to do is we want to bring this down. We're going to create a collider for the head, for the body, and then for the feet. OK, so let me go ahead and just take the x. Then we drag that down, and do the same thing for the y here. Let's adjust our center to basically be right on the body itself. OK, so something like that. OK? So that looks good. And then, we're going to go ahead and we're going to add in another collider. So we're going to add a component. We're going to do Physics2D, and we're going to do a circle collider. So you can see here that it comes in as another collider. Now, you might be used to 3D components, where you can only have one type of collider. Well, with 2D, we can have multiples. OK? So it's the same thing here. We're going to go ahead and adjust the center. We're going to put this at the feet. OK? Now, the reason that we want this here for the character is because whenever the character is going up hills, or inclines, we need this round radius here. And that's just going to help. So let me go ahead and take the radius up. And we need this to be round so that way, they can actually climb inclines. With something like this box, that corner gets caught, and it creates kind of a jaggedy motion. And we don't really want that. We want a smooth motion. So come in. Try to get this as close as possible. Something kind of along these lines. Let's adjust that center. And we want to put that right at the bottom of the feet, just like so. OK? It's OK if it's not covering up the entire bit of the foot here. That's perfectly fine. We're going to add another Physics2D. We're going to use another circle collider. And we'll make some adjustments to this one. And we're going to put this over the top of the head. So we don't have to get the entire head. We just want to get fairly close. Something like that. Maybe a little bit bigger. And we'll just adjust our radius some. And that should be close enough. OK? So we've got something like that. So now if we select our character, we've got all of our colliders. And then all we have to do is go ahead and just collapse those, because we already got those set up. We don't need to do anything else. And there we go. So now, what I want to do, is I want to add in a Physics2D rigid body. OK? Rigidbody2D. And what this is going to do is it's going to create some mass for our character, and it will drop down. So to get started with this, let's come in and we could reorder this if we wanted to. I'm going to go ahead and just leave it right here. I'm going to say Move Up. OK, you could do that. Usually I like my Rigidbodies at the top, above my colliders. So we have a mass here. Everything else is good. Gravity scale is just fine for right now. So in here, let's go ahead and check fixed angle. Because what this will do is it will keep our character upright. And then, we don't need isKinematic. Let's take Interpolate and let's just set that to Interpolate. Everything else we're going to leave as default for right now. That should be good. And let's go ahead and collapse that. All right. So if I were to come in and I hit Play, what you're going to notice is our character falls, but nothing happens here. We get a little bit of a warning here. I don't think this is anything that I have to worry about. Let's see. Trying to destroy some game objects that are-- actually this is there. I'm not sure what this is. Let's go ahead and select it, and then hit Delete on that. Let's hit Play. And it's trying to create this. I'm not really sure what that is, and we're not going to worry about that. But the main thing is that our character is falling through the level. OK? Now, we have him colliding. We have his colliders and everything, but we don't have anything to collide with. So we need our ground to collide with. So what we'll do is I'll select just one of these ground objects, and I can add it as Component, and say Physics2D, and then we're going to say BoxCollider. We'll expand that out, and then you can see that that shows up there. Now, instead of using one collider for each individual ground piece, we could just use one of these colliders. And so what I'll do is I'll go ahead and I'll just adjust the size in the x direction. OK? And try to get that to a rough size there. We might go to something around 40 or so. And then I'm going to adjust the center on the x, and we're going to push that over until that goes across the entire ground section. So let's go ahead and expand that out. A little bit more. Let's go to 42 even. And then we'll just make some adjustments to our x here. And then I'm going to lower that down on the y, so that way it's right underneath the grass. OK? So it makes quick adjustment here. Something like that. And then now, with that said, if I were to hit Play, you'll see that our character comes in and is now on the ground. So now we have our Physics all set up. Everything is ready to go. Just a couple of errors that are coming up. Some cleaned up errors when closing the scene. I don't think that's anything that we really have to worry about. It may not actually be showing up in your project. We'll try to take a look at that a little bit later. But let's just go ahead and stay on task with the rest of these. You'll want to go ahead and take note of these numbers here. So that way, you could do the same thing for the rest of these. Now, I could come in and I could copy the component, select this guy, and then right click in here. And let's see. It's not allowing me to-- there we go. Right click in there, and then say Paste Component As New. There we go. And then right click on SpriteRenderer, Paste Component As New. OK? And then one more time. There we go. With this one, you could go ahead and you could make some adjustments if you need to. I'm going to go ahead and leave that just the way it is. And then this one, we're going to do its own box collider, so add it as a new component. Physics2D, BoxCollider. Come in, make your adjustments. Make sure that the collider comes down below. And so let's center that on the y. Let's bring that down to right about there. And then also, make sure your size is set up. Make it a little bit thinner. And readjust that. So that way, it's not down below here. And then also in the x, make sure it comes out to those edges like that. And make any adjustments accordingly. So I'm going to do negative .02. OK? That gets pretty close. Actually, zero will do fine. There we go. OK. So now that is set. We've got all of our collisions, and we are now are ready to go. So characters are all set, collisions are all set. And now what we want to do is we want to start getting into animating our character. So I'm going to show you how to use the animations tools. Animate an idle and a walk animation. We may have to take a couple of lessons to do that. And then, we'll go ahead and we'll jump in how to make those blend, and then getting our character to move for the simple platform or game. OK? So we'll get started with that next.
In this series of training we will discuss the major components of working with the 2D features in Unity. We'll start off by learning how to set up a 2D Unity project. Then we'll learn how to properly import and slice our sprites. After that we'll learn how to manipulate sprites by blocking in a simple platform level and also learn the importance of Sorting Layers.

Then we'll jump into the real fun by assembling, parenting and animating a character. By the end of this tutorial, you will have a better understanding of working with Unity's 2D features.