Hands-On Unity 2020 Game Development
上QQ阅读APP看书,第一时间看更新

Creating Shapes with ProBuilder

So far, we have created simple scenes using cubes and primitive shapes, and that's enough for most of the prototypes you will create, but sometimes, you will have tricky areas of the game that would be difficult to model with regular cubes, or maybe you want to have some deeper details in certain parts of your game to get a visual of how the player will feel that area. In this case, we can use any 3D modeling tools for this, such as 3D studio, Maya, or Blender, but those can be difficult to learn and you probably won't need all their power at this stage of your development. Luckily, Unity has a simple 3D model creator called ProBuilder, so let's explore it.

In this section, we will cover the following concepts related to ProBuilder:

  • Installing ProBuilder
  • Creating a shape
  • Manipulating the mesh
  • Adding details

ProBuilder is not included by default in our Unity project, so let's start by learning how we can install it.

Installing ProBuilder

Unity is a powerful engine full of features, but having all those tools added to our project if we are not using all of them can make the engine run slower, so we need to manually specify which Unity tools we are using. To do so, we will use the Package Manager, a tool that we can use to see and select which Unity packages we are going to need. As you may recall, earlier, we talked about the Packages folder. This is basically what this Package Manager is modifying.

In order to install ProBuilder with this tool, we need to do the following:

  1. Click the Window | Package Manager option:

    Figure 4.20 – Package Manager option

  2. In the window that just opened, be sure that Packages is in Unity Registry mode by clicking on the button saying Packages at the top-left part of the window and selecting Unity Registry:

    Figure 4.21 – Showing All Packages

  3. Wait a moment for the left list of packages to fill. Make sure you are connected to the internet to download and install the packages.
  4. Look at the ProBuilder package in that list and select it.

    Important Note

    Im using ProBuilder version 4.2.3, the newest version available at the moment of writing this book. While you can use a newer version, consider that the steps to use it may differ. You can look at older versions using the arrow at the left of the title.

    Figure 4.22 – ProBuilder in the packages list

  5. Click on the Install button at the bottom right-hand side of the Package Manager:

    Figure 4.23 – Install button

  6. Wait a moment for the package to install. You will notice the process has ended when the Install button has been replaced with an Up to Date label.

Now that we have installed ProBuilder in our project, let's use it!

Creating a Shape

We will start our base by creating a plane for our floor. We will do this by doing the following:

  1. Open ProBuilder and go to the Tools | ProBuilder | ProBuilder window:

    Figure 4.24 – ProBuilder Window option

  2. In the window that has opened, click the plus icon (+) at the right of the New Shape button:

    Figure 4.25 – New Shape option

  3. In the Shape Selector, select Plane.
  4. Set Width and Length to 50.
  5. Set the Width and Length segments to 2. We will need those subpisions later:

    Figure 4.26 – New Shape settings

  6. Click the Build button to confirm the Plane.
  7. Click the first button of the four ProBuilder buttons in the Scene view to enable movement of the entire plane:

    Figure 4.27 – Select object tool

  8. Replace the placeholder cube with this floor:

Figure 4.28 – Plane subpided in a 3 x 3 grid

Now that we have created the floor, let's learn how we can manipulate its vertexes to change its shape.

Manipulating the mesh

If you select the plane, you will see that it is subpided into a 3 x 3 grid because we set up the width and height segments to 2 (2 cuts). We have done that because we will use the outer cells to create our walls, thus raising them up. The idea is to modify the size of those cells to outline the wall length and width before creating the walls. In order to do so, we will do the following:

  1. Select the plane.
  2. Click the second button (showing the vertices) of the four new buttons that appeared in the Scene View:

    Figure 4.29 – Selecting the vertices tool

  3. Click and drag the mouse to create a selection box that picks the four vertices of the second row of vertexes:

    Figure 4.30 – Vertices selection

  4. Click on the second button in the top-left of the buttons of the Unity Editor to enable the Move Tool:

    Figure 4.31 – The Move Tool

  5. Move the row of vertexes to make that subpision of the plane thinner. You can use the checkered pattern on the terrain to get a notion of the size of the wall in meters:

    Figure 4.32 – Moved vertexes

  6. Repeat steps 3 to 5 for each row of vertexes until you get wall outlines with similar sizes:

Figure 4.33 – Moved vertexes to reduce edge cell width

Important Note

If you want the vertexes to have exact positions, I recommend that you install and explore the ProGrids package. It is a position snapping system that works with regular Unity and ProBuilder.

Now that we have created the outline for our walls, let's add new faces to our mesh to create them. In order to use the subpisions or "Faces" we have created to make our walls, we must pick and extrude them. Follow these steps to do so:

  1. Select the plane.
  2. Select the fourth button of the ProBuilder buttons in the Scene view:

    Figure 4.34 – Select Face tool

  3. While holding Ctrl (command on Mac), click over each of the faces of the wall outlines:

    Figure 4.35 – Edge faces being selected

  4. In the ProBuilder window, look for the plus icon (+) to the right of the Extrude Faces button. It will be located in the red section of the window:

    Figure 4.36 – Extrude Faces option

  5. Set Distance to 5 in the window that appeared after we clicked the plus button.
  6. Click the Extrude Faces button in that window:

    Figure 4.37 – Extrude distance option

  7. Now, you should see that the outline of the walls has just raised up from the ground:

Figure 4.38 – Extruded grid edges

Now, if you pay attention to how the base floor and walls touch the terrain, there's a little gap. We can try to move the base downward, but the floor will probably disappear because it will be buried under the terrain. A little trick we can do here is just push the walls downward, without moving the floor, so that the walls will be buried in the terrain but our floor will keep a little distance from it. You can see an example of how it would look in the following figure:

Figure 4.39 – Slice of expected result

In order to do so, we need to do the following:

  1. Select the third ProBuilder button in the Scene view to enable edge selection:

    Figure 4.40 – Select edges tool

  2. While holding Ctrl (command on Mac), select all the bottom edges of the walls.

    If you selected undesired edges, just click them again while holding Ctrl (command on Mac) to deselect them, all while keeping the current selection:

    Figure 4.41 – Selecting floor edges

    Information Box

    If you want to use the Wireframe mode in the previous screenshot, click on the Shaded button in the top-left part of the Scene view and select the Wireframe option from the drop-down menu.

  3. Enable the Move Tool pressing the second button in the top-left part of the Unity Editor:

    Figure 4.42 – The Move Tool

  4. Move the edges downward until they are fully buried under the terrain:

Figure 4.43 – Overlapping faces

Now that we have a base mesh, we can start adding details to it using several other ProBuilder tools.

Adding details

Let's start adding details to the base by applying a little bevel to the walls. Follow these steps:

  1. Using the edge selection mode (the third button of the ProBuilder buttons), select the top edges of our model:

    Figure 4.44 – Top wall edges being selected

  2. In the ProBuilder window, click on the plus icon to the right of the Bevel button.
  3. Set a distance of 0.5:

    Figure 4.45 – Bevel distance to generate

  4. Click on Bevel Edges. Now, you can see the top part of our walls with a little bevel:

    Figure 4.46 – Result of the bevel process

  5. Optionally, you can do that with the bottom part of the inner walls:

Figure 4.47 – Bevel being applied to floor-wall edges

Another detail to add could be a pit in the middle of the ground as a hazard we need to avoid falling into and to make the enemies avoid it using AI. In order to do that, follow these steps:

  1. Enable the FACE selection mode by clicking the fourth ProBuilder Scene view button.
  2. Select the floor.
  3. Click the Subpide faces option in the ProBuilder window. You will end up with the floor split into four.
  4. Click that button again to end up with a 4 x 4 grid floor:

    Figure 4.48 – Subpiding the floor

  5. Select the four inner floor tiles.
  6. Enable the Scale Tool by clicking the fourth button in the top-left part of the Unity Editor:

    Figure 4.49 – Scale Tool

  7. Using the gray cube at the center of the gizmo, scale down the center tiles:

    Figure 4.50 – Inner cells being shrunk

  8. Click the Extrude Faces button in the ProBuilder window.
  9. Push the extruded faces downward.
  10. Right-click on the ProBuilder window tab and select Close Tab. We need to get back to terrain editing and having ProBuilder open won't allow us to do that comfortably:

    Figure 4.51 – Close Tab option

  11. Select the Terrain and lower that area of the terrain so that we can see the pit:

Figure 4.52 – Terrain being lowered for the pit to be visible

I know we didn't plan the pit in the original level layout, but remember that the define acronym is a document that will constantly change in the middle of game development, so sometimes, we can be bold and change it in order to improve the game. Just take care to not go too far with never ending changes, which is a difficult-to-master art.