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.
Setting up props
See Details
Joshua Kinney

Pluralsight

Joshua Kinney
In this lesson, we're going to set up our props in our 2D game. All right, so we set up our background and our foreground elements, meaning the ground itself. And now what we want to do is we want to start to add a little bit of life to our levels by adding in props. So so far with Unity, you may be used to adding in props or assets using 3D geometry. So you would add in colliders and physics and things like that. And it's pretty much the same process, there are just a few things that are a little different. So to get started with this, first thing that we want to do is go ahead and bring in one of the props that we want to use. So I'm to bring in a bush. And we're just going to left click and drag that into the scene. And you'll see that it comes in behind our background element. So this needs to go on to a sorting layer, which I've already set up, so I'm going to go ahead and drop that down. And I'm going to put this on the props sorting layer. So that's going to go above my background. But it's going to go below my foreground, OK? So if I drag that down, you can see that the grass of the ground is covering that bush up. And that's perfectly fine. The size of this is already set up. It's set up the way that I want it to be. And we can go ahead and we could place anywhere that we want, right here on our scene. So I'm going to add it right here at the beginning, OK? And then we can hit Control D, and we could duplicate this, we can hold Shift and we can pull that out. We can hit Control D again, and we can pull that out a little bit, kind of create a little bit of an overlapping effect here. And then I could also even scale this down. So you can grab it by one of the corners, and you can drag that down. And that will scale it uniformly. Normally what I like to do is, I like to type in a nice even number, that's around what I have. So 0.75 is a good value there. Now remember, with your props, and your different elements, you need to adjust your sorting layers. The reason that we need to adjust sorting layers and their order is because we can get what's called z-fighting. Now we can't really see that here in the 2D view, but if I were to click off of the 2D view and go to 3D, watch what happens once I start to move around. You can see some of the pieces begin to pop. Pay attention to the bush here, let me go ahead and deselect that. Watch the bush as it pops in and out, OK? That's because of the z-fighting issue. And what that is, is it's saying that the camera is not necessarily sure of which one to draw first. And whenever we have objects that are in the same sorting layer, we need to make sure that we set up the individual objects in that sorting layer in their own order. So this can be done by selecting the object, and going to Order in Layer. So for this guy, we want him to be drawn in front of the larger bush. So we're going to set his layer up to 1. And now, no matter what happens, that is going to be drawn in front. Because now it knows what order it's supposed to go in. Now you'll notice that the ground pieces, those are flickering back and forth as well. So what I'm going to do is, I'm going to just every other one, and we cannot select it here in the scene view, because our layer is locked. So let's unlock that foreground layer. Let's select every other ground plane. So I'm going to hold Control, and I'm going to select those, and then I'm going to set those to Order in Layer. I'm going to set that to 1. Now by doing so, you'll see that there's no more flickering between those elements. Let's do the same thing for the rest of these. So select every other one, holding Control. Set that to Layer one, there you go. And let's come in and let's select this one, hold Control, should have three. Set up to 1. Whoops! Let's do 1. This one should be fine. And let's do this one. Hold Control in this one. There we go. So you might be asking, well if it wasn't doing anything in the 2D view, and I'm never going to be in that 3D view, does it really matter? Um, you don't want to take that chance. It's always just a good practice to set it up to where you have your sorting layers in the proper place, and then you have your individual objects in the sorting layer ordered properly as well. So just don't take any chances on that. Just do nice, good practices. OK, so now let's add in the last prop here. And that's going to be this rock, so I'm going to drag and drop that in. And you'll see that that's coming in behind as well. Let's go ahead and add this to our props layer. And that is placed properly. Now we can place this anywhere that we want in our level. And these are going to be obstacles for our player. So they're going to have to jump over these objects. They can go right in front of the bushes, OK? So we can hold Control and D, we can drag that over to the other side here. And again, you can layer those over the top of one another, you can scale them, just remember to order those properly in your layers. OK, so now that we have these props in, let's talk about collisions. So the bushes themselves, I don't really need any colliders for those. Those are just there to break up just the flatness of our ground. Just to add a little bit of flair. Now the rocks themselves, they are also there are to break up just the flat ground. But they're also there to be obstacles for the player. So my player is going to have to collide with those. So what I need to do is, I need to set up a collider for this object. So with this rock, let's go ahead and add that in. So we're going to add a component, and we're going to go to Physics 2D. And then here we can add in any type of collider that we want. Each one is going to take its own specific shape. So we have the circle collider, we have a box collider, and edge colliders, and so on and so forth. And then we have these joints. We're not going to worry about those for right now. So looking at this rock here, if I were to use the circle collider, you can see that a lot of that is going to be cut off. I can scale down that circle collider by going to my radius, and I can drag that down, but you'll see that it's not colliding with all of the rock. Let's go ahead and change it to something like a box collider. So I'm going to remove that component by right clicking on it. Add a new component. And let's do the box collider. Now the box collider is taking that shape, and we can come in and we can adjust its size as well. And this gets a little bit closer, but a lot of it's still getting cut off here. And I don't really want that. So for this particular object, it might be best to use a polygon collider. Now polygon colliders are going to be a little more expensive, but sometimes they're needed. So let's add component, physics 2D, polygon collider. Here you can see it creates a green collider that takes the shape of our rock. And the reason that it's more expensive is because it has more points on it. Now what we want to do is we want to come in and we want to reduce the number of points that are being used. And there might be some unnecessary points. So a way that we can start to adjust this is by coming in and holding down Control, and you can see that as I hover over vertex on that mesh, it turns that vertex red. And if I left click on that, it's going to remove that vertex. So now I can just come in and start removing these. I don't want that on the grass at all, so that's just going to get rid of that. I can come in, I want that one. We've got quite a bit of a mess right in here. So I'm going to come in and start removing some of those points. Now not all points are going to be able to be removed. Now because what it's doing is, it's trying to calculate the points that we have already. Now what we can do, is we can come in and hold Shift, and we can move those points. So if I move this out here, you can see that it starts to change the overall shape of this a little bit more. So it's trying to re-triangulate everything. So this triangle here in the middle, we really can't get rid of it. But what I can do, is I can hold down Control, and I can get rid of some of these across the top. And that will start to reduce that size as well. Let's hold Shift, and let's just reshape this. And you want to try to optimize that as much as possible. So if there's an error, a vertex that's just not needed, go ahead and hold Control and remove that. So Control click. So I've got these on the grass, these are not needed. So the less points the better. This one we can move up to here, just like so. And you'll see that it creates another line right here. You can hold down Control and remove that completely, and that will work just fine. So you may not be able to get all of these, but we can get fairly close. So I'm going to pull this one down, and what it's trying to do is, it's trying to create the polygons for all of this. And so sometimes it has to create a triangle in the midst of all of this. So we can come in, we can move those points around, try to get as few points as possible. That looks pretty good there. I think this will do. We could probably get rid of a couple more of these. But we just want to make sure that we're following along with the surface of the texture, of the sprite. We could probably do without that one there. And I think that is going to do pretty well. Let's go ahead and leave it with that. If you want, you can expand your collider info, and you can see how many vertices you have here. If you want to reduce that even more, you could do so. But I think that's going to do it for us. OK, so we're down to 14. Looking good. All right, so now from here, once you have those colliders already set up with your rocks, you don't have to set that up with another one. So for example, we have this one. Instead of resetting it, let's just delete it, and then duplicate this one. Control D, hold Shift. Actually, left click on it first, then hold Shift and drag it over. If you hold Shift, you might move one of the points on the mesh around. We don't want to do that. So left click on it first, hold Shift, move it across. Or you could just put it in the position just by moving it that way. Either way is fine, OK? So there are our props. What I want you to do in between lessons is, I want you to come in, add in just a couple more rocks in these areas. I'm actually going to add one more here. Let's hit Control D. And let's put that right here. Because the player is going to need to jump up onto this rock, and then get up into this area here. So I could do this by scaling that up. Something like that. And then let me hit Control D, and I'll put a smaller rock here. Let me scale that back down. Let me actually type in 1 on that one. And then remember, if they're overlapping one another, what you want to do is you want to make sure that they're set in their order layer. So we're going to set this 1. There we go. So now we won't have a z-fighting issue, no matter what. OK, so go ahead and set up the rest of those bushes. Set up some more rocks if you want to. And then in the next lesson, what we're going to do is we're going to get started with bringing in our character, and setting up the collisions, and getting the game started here. 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.