Planting Cherry Trees

In Chapter 4, Creating Our Terrain, we created a tree to demonstrate how to create one from scratch. Since we downloaded a Cherry Tree in the last section, we no longer need our experimental tree. In the hierarchy panel, delete the three we previously created. Next, delete any tree assets and tree-related texture folders in the project panel. This will help us stay organized and minimize the size of our Unity game project.

Basic trees in Unity can be painted on a terrain. To accomplish this, you start by orienting the scene view so that you have a top-down view of your terrain. Then, in the Hierarchy panel, select your terrain. You then use the Place Trees button in the Terrain component of the Inspector panel to paint the trees on your terrain.

Because our Cherry Trees are special and contain multiple meshes, we cannot use the terrain painting tool. Instead, we will make multiple copies of our Cherry Tree prefab and place them where we want them to be in our game world using the previously provided diagram as a reference.

We have a few preparatory steps to take prior to planting our Cherry Trees. Follow these steps to prepare the prefab:

  1. In the Project panel, right-click on the Prefabs folder and select CreatePrefab. Name the prefab CherryTreeCollider.
  2. In the Project panel, drag the CherryTree.prefab that you imported in the previous section into the Scene view. It doesn't matter where.
  3. In the Hierarchy panel, select the Cherry Tree you just added to the Scene view.
  4. In the Inspector panel, click the Is Trigger checkbox in the Box Collider component.
  5. Still in the Inspector panel, click the Add Component button and then select Physics | Box Collider. We will use this collider to help us determine when the Cucumber Man is collecting cherries from a Cherry Tree.
  6. Next, we will edit the collider to ensure it encapsulates the entire tree. In the Box Collider area of the Inspector panel, click the Edit Collider button.

As you can see from the following screenshot, the collider is at the base of the Cherry Tree prefab:

  1. Using the the squares of the box collider in the Scene view, enlarge the collider so that it encompasses the majority of the Cherry Tree. It does not need to include the tree trunk. An example configuration is shown in the following screenshot:
  1. In the Project panel, ensure that the Prefabs folder is selected.
  2. Drag the Cherry Tree from the Hierarchy panel to the CherryTreeCollider prefab in the Prefabs folder. This establishes a Cherry Tree prefab with a box collider.
  3. In the Hierarchy panel, delete the Cherry Tree, unless it is in a place that you want it to be in.
  4. Select the CherryTreeCollider prefab in the Prefabs folder. 
  5. In the Inspector panel, click the pull-down menu next to Tag. Select the Add Tag button.
  6. As indicated by the arrow in the following screenshot, click the + icon at the bottom of the empty list:
  1. In the pop-up window, enter CherryTree as the tag name and click the Save button.
  2. Select the CherryTreeCollider prefab in the Prefabs folder. 
  3. In the Inspector panel, click the pull-down menu next to Tag. Select the CherryTree tag.

Now you are ready to start planting your Cherry Trees. Simply drag the prefab to each area where you want a Cherry Tree. You can create as many or as few as you want. The example provided with this book contains 25 Cherry Trees.

Once your Cherry Trees are where you want them, your hierarchy panel will probably be disorganized. A quick trick to make things more organized is to right-click inside the Hierarchy panel and select Create Empty. Then, rename the GameObject to Cherry Trees. Lastly, in the Hierarchy panel, select all the Cherry Trees and place them in that GameObject. Now that GameObject serves as a folder for viewing purposes. You can collapse and expand the folder as needed.

If you placed your Cherry Trees using a top-down view in the Scene view, some of your trees might need to be raised or lowered based on your terrain. You can double-click a Cherry Tree in the Hierarchy view to auto-zoom in on the Scene view. 

Once you complete your work on the Cherry Trees, save your scene and your project. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset