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 prefabs
See Details
Joshua Kinney


Joshua Kinney
In this lesson, we're going to learn how to create prefabs to make assembling our level much faster. All right. So we're picking up right where we left off with our three game objects that we imported in. And now what we want to do is we want to assemble these into what's called prefabs. Now, a prefab can be thought of as an object that can be pulled from the Project Files folder and duplicated many, many times. So let's go ahead and start lining everything up and creating the final look that we want for this piece. Now, we're going to be creating the mineshaft straight piece. And we're going to create a couple of other variations using these three pieces, and I'll show you how to do that in this lesson. So the first thing that we want to do is we want to create a game object that is going to really hold the pivot point of these game objects here. So to do this, I'm going to create an empty game object. Let's go ahead and rename it. And I'm going to use the prefix that I like to use for prefabs, and that's PFB, and then underscore, I'm going to call this Mineshaft underscore Straight. Now, with that game object in the scene, we want to reset its positions or its transform to 0, 0, and 0. It just makes things easier to line up. So I'm going to do that by clicking on this gear under our transform and hitting Reset. Now, what that will do is reset your position and rotation to 0 all the way across, and then reset your scale to 1 all the way across. So now with that said, let's take the floor here, and let's do the same thing. Let's reset that. And then now we have a good place to start assembling the walls and the ceilings too. So let's select this wall, and let's line it up with this edge of our floor. Now, we could do this by selecting the x-axis on the Move tool and pulling that over and getting in as close as possible, and then pulling it over in the z. But we may not be exact, and we want to be exact with this. So a great way to snap objects together is by using what's called a vertex snap. And this can be done by holding down the V key with your Move tool activated, and you'll notice that the move gizmo is moving to the closest vertex based on my mouse's position. So if I come down to this bottom corner and then I left-click and hold, and I drag up to the vertex that's closest to the other game object that I want to snap to, you'll see that that object begins to snap. So let's pull it over to this corner here and release. Now let's do the same thing for our ceiling. Select it, hold V, select that corner, left-click and hold on your left mouse button, and drag that to that corner. And there we go. So we have this started, but we need the other wall. So let's select this will, hit Control-D to duplicate, and then I'm going to hold Control and left-click and drag on the x-axis, negative 5. And I can see that here on our position. Now, it looks like our wall has disappeared, but we're just looking at the back side there. So let's rotate it around 180 degrees. Now, I could do this by using my Rotation tool, holding Control and dragging that around, or I can simply type it in here. I could type in negative 180 and hit Enter. And there we go. So there is our straight piece for our mineshaft. Now what I want to do is I want to take all four of those pieces, so let's hold down Control and select all four of those pieces, and let's group that or parent them into the mineshaft straight. So left-click and drag, drop that. And now if we select that, we can now rotate and move that piece all as one. All right. Now, it's still not a prefab. OK? A prefab belongs in the Project Files folder. And so to do that, we're going to create a new folder right here on our Assets folder, and we're going to call that Prefabs. Hit Enter on that. And double-click on your Prefabs folder, and then we're going to right-click, Create, Prefab. Let's give it a name, and I'm going to call it PFB underscore Mineshaft underscore Straight. Hit Enter. And then to give that some information, or give it geometry for the prefab, we're going to left-click and drag what we created here in the scene onto our prefab in our Project folder. Notice that it's gone from that white cube icon to a preview of our prefab. So now that we have that set as a prefab, let's go ahead and create the colliders that we're going to need for this game object. Now, we need colliders, so that way our first person player controller, which we'll bring in here in just a moment, will be able to walk on the floor and then bump into the walls. So to get started, let's select the floor. And we could do that here in the Hierarchy panel, and we can go to the floor right here. And then we're going to scroll down, and we're going to go to Add Component. Let's go to Physics, and then Box Collider. Now, our box collider looks pretty good for the most part. We need to do a little bit of sizing on this. So in my y direction here, I'm going to go ahead and type in 0.1. That gives It some thickness, so that way it's not too close that it can't calculate the collisions with the player. We need just a little bit of thickness there. Let's do the same thing for the walls. So select the walls, Add Component, Physics, Box Collider. Now, on this one, we're going to adjust the x direction, so type in 0.1 and hit Enter. Let's select this one, do the same thing, Physics, Box Collider, 0.1 on that x direction. And then we could also use a collider on the ceiling. Now, we don't necessarily have to because the player's never going to get up that high, but just in case. Let's add a component, Physics, Box Collider. And you'll notice that the size on this one has already set itself up. And usually it tries to do it automatically, but whenever we have an object that doesn't really have any thickness, it has a hard time adjusting that thickness there. So that's why we had to type in the 0.1. Now that we have our colliders on our game objects here, we need to make sure that that is updated into our prefab that's in our Project folder. So left-click on the game object here, and then we're just going to simply hit Apply next to the prefab. And what that will do is it will save any changes that we've made in the scene to the prefab that is in the Project folder All right. So now that that has been done, let's create one more piece. So let's hit Control and D to duplicate that. I'm going to grab my Move tool, hold Control, and then drag that out. And what I want to do is I want to create another wall here on the end. But I'm going to go ahead and just take this wall here and move it into position here so we can create a corner. So let's go ahead and change our y value from negative 180 to negative 270. And then with our Move tool selected, let's hold V, excuse me, left-click and drag to that corner. And there we go. Now, again, this is not a prefab. We need to create a new one. So we're going to right-click, Create, Prefab. We're to call this PFB underscore Mineshaft. And actually, let's rename that one more time. Let's go all the way to the end, Mineshaft underscore Corner, and hit Enter. Let's take this one. Let's make sure that we rename it, so that way we can see it reflected here. Because whenever we create a prefab, the names probably won't match up, so we need to make sure that we change them on both. Let's left-click and drag that onto here, and there we can see that's updated. This is called Mineshaft Corner, and we've reflected that with a name change here. All right. So now I want to show you how to bring in a character that we can use to test out our prefabs. So what we'll do is we'll right-click on our Assets folder. We're going to Import Package, and we're going to import the Character Controller. So let's go ahead and just hit Import on that. And once that's done, you should see a Character Controller folder that we can bring in. So Standard Assets, Character Controller, and you'll see the third person controller and a first person controller. We're just going to use the first person controller for this project. Left-click and drag that into the scene, and then lift it up. Now, this first person controller here is considered a prefab. Notice the blue icon here, and that's telling us it's a prefab there. So now what we can do is if I hit Play, I can move around in my scene using my first person controller. Now, I know that it looks a little weird with all the flat gray and the flat blue. That's because we don't have any lights in the scene. But you can see that we are colliding with the walls and the floor. So everything's looking great, and everything's working out just fine. Now, we're going to need to create one more prefab, but I want to give you a little bit of practice. I want you to create a prefab that is an end. So I want you to take this game object, the corner, and I want you to create the wall that would go right here. So that will just give you a little bit of practice on how to go about doing this. All right. So now in the next lesson what we're going to do is we're going to go ahead and add materials to our game objects, and then we'll show you how to create materials and how to add those. And we'll get started with that next.
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.