What you will learn
In this Unity tutorial, we will learn how to effectively utilize the game engine. Over the tutorial, we will learn how to use the Unity editor interface as well as how to navigate in the scene view. From there, we will learn how to import assets and how to create prefabs. Then we will shape our level even more by adding character controllers, colliders, lights, materials and physics. We will then finish the tutorial by learning how to use scripting in order to make our level interactive.
In this lesson, we're going to start by learning how to create prefabs inside of the Unity game engine to make it much easier to build complex levels. So here's where we left off in our last lesson. And you can actually load the [? lessonseven.unityscene ?] to get to this point. So if you want to find that, just go to the Lesson Scenes folder and you can actually just load that scene right up here. All right, so at this point what we need to do is actually create what's called a prefab. And we may have some shapes that we're going to use several times inside of our level here. So I have a floor object, a wall object, and a ceiling object. Now, I don't necessarily have a wall on the inside, and I do that for a specific reason. Because I want to be able to interchange this wall as I go. But I'm going to be using this particular combination several times throughout this level. So I want to go ahead and make a prefab so that way I don't have to keep creating a floor, and then a wall and snapping that, and then creating a ceiling and snapping that and bringing that down. So this makes things go a little bit faster. So to create a prefab, there's a couple of things that you need to keep in mind. The first thing that you need to keep in mind is OK, what do I need on this prefab to actually make it work? Now I'm going to use a couple of terms here that we haven't talked about at all, so just bear with me. Because we're going to cover those in detail in later lessons. So the first thing that I need on my level is colliders. I don't want to fall through my level whenever I create a character controller and start playing around in this level. So before you create prefabs, you need to make sure you have all of that stuff set up. Right now I have materials already on these objects, and they're just standard gray so I don't have to worry about materials right now. But I do need to go ahead and worry about those mesh colliders, I need to make sure that that is in there. So if I go to My Component-- now we're going to get into adding components later in the lesson, so I'm just going to kind of go through this very quickly. I'm not going to explain this in great detail. So I'm going to go to Physics, and I'm actually going to use a Mesh Collider here. Now it's saying, hey, you're losing your prefab. Now I know that we just said we're going to be creating some prefabs here, and it's saying that we already have a prefab created. Well, technically, yes we do. You see ceiling_1 here, see how it's in this blue color text. This is considered a prefab. That means that it's unchanged from our project folder here. Now once we start adding components to it, that's going to change it from a prefab to just a regular game object. Now we're going to create a brand new prefab from this combination of objects. So this warning, don't worry about it. We're just going to go ahead and add that component. So now you see that mesh collider is now in our inspector of our floor object. So what we're going to do now is go ahead and do the same thing to these objects here as well. Now I'm just going to do it to the wall. I don't necessarily need it on the ceiling, because I don't have to worry about hitting the ceiling and jumping through the ceiling and that sort of thing. So let's use Mesh Collider again. Again, we're going to get this warning box, and we're just going to hit Add on it. So now what we need to do is go ahead and create what's called a prefab. So what I'm going to do is, to keep things nice and neat, I'm going to right-click inside of my project view here and Create Folder. And I'm going to call this Prefabs, just to to keep these nice and neat. So inside of my prefabs folder I'm going to go ahead and create a brand-new prefab. So let's go to Create and Prefab. Now you'll see here that we have new prefab, let's go ahead and rename it. So I'm just going to call this hallway_straight. And I'm going to do _1, just in case I'm going to make several combinations of hallway_straight-- just in case. So once that's renamed, we'll deselect that. And you'll notice that we get this white cube icon next to this prefab. And this white cube means that it is an empty prefab, there's no objects in it. Now it's really important here that you have the project view, or the project tab here, separate from the hierarchy tab. So if you went ahead and you created a custom layout and you liked that the project view and the hierarchy view were together like this, this isn't going to work. Because we can't drag stuff from our hierarchy into our project view. So I'm going to go ahead and separate those again. So let's go ahead and start creating this prefab. First thing I need to do is actually start parenting these objects together. So let's select our floor, and I'm going to actually parent my wall and my ceiling to this floor object. So let's go ahead and grab our ceiling, and we're going to click and hold and drag this onto floor_1. So what you should see now is a little arrow next to floor_1. Let's expand that, and you'll see ceiling_1 together. So let's do the same thing for wall_1, and let's bring that right on top of floor_1 as well. So now wall_1 and ceiling_1 are now parented to floor_1. So if I minimize that, and let's select this floor, you'll notice that the wall and the ceiling are selected together. So now I can actually move these objects together. So I'm going to hit CTRL+Z one time just to bring it back to that initial position. CTRL+Z is undo. So now we've created a prefab, but we haven't actually completely made it a prefab just yet. Once they are linked together, we need to go ahead and link it to this prefab object. So let's go ahead and click and drag our floor_1 right on top of hallway_straight_1. Now you'll see that this box is now blue. This means that it has objects tied to this prefab. So here we see wall_1 and ceiling_1. Now you're probably asking yourself OK, now where is the floor_1? Well, actually hallway_straight_1 has replaced the name of floor_1. I know that's getting a little bit confusing, but it's still exactly the same. So what I'm actually going to do with this floor_1 is actually go ahead and just change it to this name right here. So I'll just type that in there. And what I'm doing right now may seem like there's no point to it, but what I'm doing is project management as I build out this level. So this is extremely important whenever you get into very large levels and projects inside of Unity. So what we've done is we've created our first prefab here. So let me try to change that name one more time there. I don't think I hit enter there, so we'll do hallway_straight_1. There we go, now it's there. So this object is exactly the same as this prefab that's in our project folder. So now what I can do is I can go ahead and duplicate this if I want to, or I can go ahead and drag a brand new hallway_straight inside of here. Either way, it's exactly the same. So now I can go ahead and snap these two objects together. I'm going to use my vertex snapping by holding down V, hovering over a vertex and I want to snap, and then I'll click the left-mouse button and hold it down, and then drag to the vertex I want it to snap to. So now these objects are completely snapped together, nice and pretty, and nice and neat. So here we've created our first prefab, and what we're going to do in our next lesson is actually go in and start creating some more prefabs, and start creating our level. So we're going to take a couple of lessons to actually get our base level out here and created. So we're going to do that in our next lesson.