Unity 2020 By Example
上QQ阅读APP看书,第一时间看更新

Improving the scene

The collection game is making excellent progress. We now have something playable insofar as we can run around and explore the environment in the first person. However, the environment could benefit from additional polish. Right now, for example, the floor meshes appear suspended in mid-air with nothing beneath them to offer support. It's also possible to walk over the edge and fall into an infinite drop. And there's a distinct lack of coins in our collectible coin game. We'll fix both of these issues in this chapter, starting with adding a water plane to support the islands.

Adding a water plane

To add water, we can use another ready-made Unity asset included in the Project panel:

  1. Open the Standard Assets/Environment/Water/Water/Prefabs folder.
  2. Drag and drop the WaterProDaytime asset from the Project panel into the scene. This appears as a circular object, which is initially smaller than needed:

    Figure 1.40 – Adding water to the environment

  3. After adding the water prefab, position it below the floor and use the scale tool to increase its planar size (X, Z) to fill the environment outward into the distant horizon. This creates the appearance that the floor meshes are smaller islands within an expansive world of water:

Figure 1.41 – Scaling and sizing water for the environment

Now, let's take another test run in the game tab. Press Play on the toolbar and you should see the water in the level. Unfortunately, you can't walk on the water, nor can you swim or dive beneath it. If you try walking on it, you'll simply fall through it, descending into infinity as though the water had never been there. Right now, the water is an entirely cosmetic feature.

The water is a substanceless, ethereal object through which the player can pass easily. Unity doesn't recognize it as a solid or even a semi-solid object. As we'll see in more detail later, you can make an object solid very quickly by attaching a Box Collider component to it. Colliders and physics are covered in more depth from Chapter 3, Creating a Space Shooter, onward. For now, however, we can add solidity to the water by first selecting the Water object from the Hierarchy panel (or in the scene view) and then by choosing Component | Physics | Box Collider from the application menu:

Figure 1.42 – Attaching a Box Collider to a Water object

Attaching a component to the selected object changes the object itself; it changes how it behaves. Essentially, components add behavior and functionality to objects, making them behave in different ways.

When a Box Collider is added to the water, a surrounding green cage or mesh appears. This approximates the volume and shape of the water object and represents its physical volume, namely, the volume of the object that Unity recognizes as solid. You can see this approximation in Figure 1.43:

Figure 1.43 – Box Collider approximate physical volume

If you play the game now, your character will walk on water as opposed to falling through. True, the character should be able to swim properly, but walking might be better than falling. To achieve full swimming behavior would require significantly more work and is not covered here. If you want to remove the Box Collider functionality and return the water back to its original state, select the Water object, click on the cog icon on the Box Collider component, and then choose Remove Component from the context menu, as shown in Figure 1.44:

Figure 1.44 – Removing a component from a GameObject

With the water set up precisely as we want it, we'll move back on land and start creating the coins for our game.

Adding a coin

At this point, our game has an environment and a method of navigation. But we're missing a core feature of our collection game – there are no coins for the player to collect! So far, we haven't had to write a single line of code. However, in order to implement collectible coins, we'll need to do just that. We'll write the C# scripts in the next chapter, but we can get started here by creating the coin object itself. To do this, we'll use a Cylinder primitive that's scaled to form a coin-looking shape:

  1. Create a cylinder by selecting GameObject | 3D Object | Cylinder from the application menu:

    Figure 1.45 – Creating a cylinder

    Initially, the cylinder looks nothing like a coin. However, this is easily changed by scaling non-uniformly on the Z axis to make the cylinder thinner. Switch to the scale tool (R) and then scale the cylinder inward so that it looks similar to Figure 1.46:

    Figure 1.46 – Scaling the cylinder to make a collectible coin

  2. By default, the cylinder is created with a Capsule Collider as opposed to a Box Collider. As you scale the object, the collider will also change size. However, sometimes it may not have the dimensions we would like. You can change the size of the Capsule Collider component by adjusting the radius field from the Inspector panel when the coin is selected. Alternatively, you could remove the Capsule Collider altogether and add a Box Collider instead. Either way is fine; generally, choose the simpler shape where possible. The colliders will be used in a script in the next chapter to detect when the player collides with a coin:

Figure 1.47 – Adjusting the Capsule Collider for the coin

We now have the basic shape and structure for a coin. We will improve it in the next chapter by making it collectible and assigning a material to make it look shiny. However, by using only a basic Unity primitive and scale tool, we were able to generate a shape that resembles a coin.