Join Adam Crespi for an in-depth discussion in this video Generating colliders on geometry, part of Unity 5: 3D Essential Training.
- Importing assets is the first step in getting things ready to be placed into a game. Before we bring our objects actually into our scene, building them up in a game environment, we need to think about the colliders that are on them and eventually, make prefabs that we can reuse over and over Our goal here is to optimize gameplay as much as possible and we do that with some careful planning of our assets and a modular design is a huge help in this. Anytime we can define something once and reuse it, we can lower our number of draw calls, how many times we have to draw or display a particular object on screen.
I've set up this mesh in a modular fashion, so we have doors, for example. We have archways the doors fit into. We've got things like conveyor belts that we can string together and there's gears we can place on the wall, pipes we can put together to make pipe assemblies, and so forth. And the idea is that if we define each of these once, we can stitch them together to make a number of environments for our game. I need to get colliders on first and I'll take a look at some of my doors to start. When we bring in an object, we have the option in here to generate colliders and typically I'm going to leave that unchecked and here's why: If we look at our components, choosing Component, Physics and colliders, we can see in here that we've got different colliders available: boxes, spheres, capsules and so forth.
And also mesh colliders. Mesh colliders are the most expensive computationally; they take the most thinking about by the game engine and so the more mesh colliders we have, the slower our game can go. So what we want to do is generate colliders on our objects and think about optimizing as much as possible. But I'll start with simply generating colliders. I'll pick my Door_01 and you can see in here I've got Generate Lightmap UV's checked and Import Materials unchecked. I've done this for all of my objects, bringing in that whole structure from the 3ds Max project, where I've got all of my different meshes sorted out in folders, in their meshes folder in the Assets.
Now I'll take Door 1 and drag it into the Hierarchy. I'll pull open the scene window a little more and we can see that door at 00, where it's exported from. I'm going to get some simple colliders on, opening up that door to reveal that it's actually 3 objects: door frame, door left, and door right. To attach colliders then, you pick the object and add on a component and there's two ways to do that: we can either choose Component, Physics in this case, and Box Collider, for example, and now we see a box collider component on that object in the Inspector or we can do it by clicking add component here in the Inspector and again choosing Physics and Box Collider.
What we want to do is go through and get our colliders on, using as optimal form as possible. So I'll start the doors with box colliders. Going into each door set, dragging it across into the scene just as a temporary working placement, opening it up and onto the doors themselves, adding on box colliders. There's other objects in here we can use different kinds of colliders on. Again, looking to optimize as much as possible, before resorting to a mesh collider. I've got both of my doors for Door 1 and Door 2, with colliders on and I'll go take a look at some of my other objects in here.
For some things, we may not need colliders at all. Our gears, for example, are really meant to be placed on the wall and they'll just sit there and spin. They've got lightmap UV's generated and no materials applied, but I'm gonna leave colliders exactly off, as we simply can't tell if we collide with them. I'll look under props, and there I've got a barrel, broom, and so forth. Some things, like the cart, will do very nicely with a box collider. I'll take that cart and pull it into the scene and wherever it is.
Drop down and choose Component, Physics, and Box Collider. Other things may want a different kind of collider on them. For example, we may want a fire extinguisher that we can kick around or knock over. For that, again I'll drag it into my scene, pull it over so I can see it, press "f" to frame in on it and for this, I'm going to use a capsule collider, choosing Component, Physics, and Capsule Collider. This starts out a capsule collider on that object, size as well as it can to it as a sphere.
We can edit this later, but getting that collider on is the big deal. We can see with each of our colliders that they have their own parameters. Is it a trigger? Is there a physic material? Where is the center and is there, in this case, a radius, height, and direction? Well we can come back and change these as we need. The big deal though is to get colliders on everything and to really think through, does this object need a collider or not? A broom, for example, will do very nicely with a box collider. Again, pulling it into the scene, pressing "f" to frame in, orbiting around with all to the left mouse and choosing Component, Physics, and Box Collider.
I'll look through and see if there's anything else in here that needs a different collider type. Some things like the filigree are meant to be up high and we'll simply not need colliders on them. Other objects like the pipes might be along the wall, but then again, it might be out in the middle where we do actually need some different collision on them. Things like the pipe cross then, pulling this in, may need two boxes. What I'll do, is to pull this over, press "f" to frame in, and choose Component, Physics, and Box Collider.
We may want to reduce this down to two boxes that are a bit skinnier. As an alternate, we can try using on this, and that was Ctrl + Z for undo, a spherical collider. Choosing Physics, and Sphere Collider. It's pretty good. This will keep us away so we don't accidentally bump into it and maybe see a flant clip into our view. It's a nice, medium in here where we've chosen a simple volume, a spherical item to generally bound this object, keeping it out of the player's way so we don't collide with it and optimizing as much as possible, recognizing that in a scene there may be three, four, ten, twelve of these, quite a few.
So anywhere along here we can optimize what we're doing in colliders, we should. Our goal, remember, is that this game plays smooth, that we're optimizing what we're doing as we go to keep our frame rate high and our draw calls low so that the player experience is smooth and seamless as they run around our game.
- Setting up the project
- Creating a player controller
- Importing assets, including models, audio, and textures
- Generating colliders
- Creating prefabs
- Applying materials
- Creating animation
- Designing a basic game level
- Lighting the level
- Creating particle systems
- Adding audio
- Building the game for desktop or mobile deployment