Before getting to the business at hand, it's worthwhile to revisit the idea of Packages. In Unity, we have seen how to import meshes that become GameObjects that have attached to them materials (and other components). These GameObjects can also have scripts and other functionality hung on them as additional components. When a GameObject has scripts attached to it, or special shaders to define the visual appearance, it can be a fairly robust asset. To move these robust assets from project to project, Unity has developed this idea of packages that contain decorated and configured GameObjects. When the project was created, we imported the Character Controller packages that included the prefab GameObjects of First Person Controller (which has already been used in the scene). At the time the project was created we refrained from adding other packages to keep the project lean and mean.
At this point, however, there are some very sophisticated and beautiful-looking prefabs that Unity has created that would be useful to us here … specifically the water. In this tutorial we will look at importing these additional assets via packages and place them in our scene.
Importing Packages
Step 1: Open Unity. Make sure to set the project to Incursion–Unity. Step 2: Import Water packages. Choose Assets>Import Package>Water
(Basic or Pro depending on which license you have). If you have Unity Pro, import the Water (Pro Only) package. If you are using the free Unity, use
Water (Basic).
Why?
What's the difference? Unity Pro includes some render to texture effects that allow for some more visually sophisticated water. Although you can import both packages (which I do for screen shot purposes here), if you do not have a Pro license, the functionality of the Pro water will not be seen.
Step 3: After Unity decompresses the package, press Import in the
Importing Package window that will pop up. Unity will then bring up an Importing Assets progress bar. When done, a new Water (Pro Only) folder or Water (Basic) folder will appear as a child of the Standard Assets folder within the Project panel.
Water
Step 4: Explore the water prefabs this process has brought in. In the
Project panel, expand the Water folder and click once on one of the prefabs that has been imported (either Daylight Water/Nighttime Water or Daylight Simple Water/Nighttime Simple Water).
The Inspector will explode with information. Notice that the GameObject will have some familiar components (Transform, Mesh Filter, Mesh Renderer) but will also contain a Water (Script) component and a very complex shader with lots and lots of inputs (especially if using the Pro version). This is the benefit of bringing in a package—all the appropriate textures that make the water look and move like water have been attached to the shader where they belong, and the script that further helps the effect has been attached and configured (Figure 6.1).
Step 5: Place the water in the scene. From the Project panel, drag one of
the Daylight waters into the scene view (Figure 6.2).
Why?
Yes, the water could have been placed by dragging from the Project panel to the Hierarchy panel. However, by dragging directly into the scene, the water can be roughly positioned within the space. The results, however, are the same as dragging to the Hierarchy panel.
Step 6: Move and scale the water to approximate Figure 6.3. The idea here is to make the water big enough so the edge cannot be seen inside the tunnel, and expands out past the edges of the dock enough so the edge is
Figure 6.1 The settings of the Daylight Water prefab as brought in via the Water package.
Step 7: Play the game to check out the new water. Press the Play button
(or press Ctrl-P on the PC or Command-P on the Mac) and walk around the scene to see how the water looks.
Why?
Don't worry too much about how this looks quite yet. It's very likely that in the game view the water looks way too blue (even if you are using a Pro license). This is because the water is reflecting the sky, which at this point is just a flat blue. It will be prettier as we define the environment further.
Skyboxes
Skyboxes are interesting things. In effect they paint a sky behind everything in your scene. The sky behaves visually as the real sky does (although generally not animated). There are a few things that are a little unintuitive about them though. First, a skybox is not a GameObject—it can't be seen in the Hierarchy panel. Skybox materials are chosen in the Render Settings (of all places), and then Cameras have the option of whether to draw them or not. Second, skyboxes don't affect any sort of lighting or baking. They are images that looks
Figure 6.2 Placing a water prefab by dragging from Project panel to the Scene view. Note that this is using the Pro version of water (real-time reflection).
like the sky (and this sky shows up in reflections if you have Unity Pro), but don't really do anything to the scene.
Still, even though it's a bit unintuitive to track down where a skybox is, it really does help take a scene out of that flat look that the solid blue background gives it. In the next few steps we will set up a skybox to see how it works and what it does for the scene. However, this is an exploration of the tool—and in steps soon after that we will eliminate the skybox to give the scene a much more ambient effect.
Step 8: Import Unity's skybox package. Unity, as part of the standard
packages that it ships, includes some fairly nice skyboxes. While custom skyboxes can be created, for now choose Assets>Import Package>Skyboxes. Again, after importing, the Project panel will show a new folder called Skyboxes within the Standard Assets folder.
Step 9: Examine any of the skyboxes. In the Project panel, expand the
Standard Packages folder, and then expand the Skyboxes folder. Notice that the skyboxes contained there are not prefabs or GameObjects at all. That circle icon represents a material. If any of these materials are selected, the Inspector will show that the skybox material actually consists of six images for each side of the box (Figure 6.4).
Step 10: Apply a skybox material in the Render Settings. Select
Edit>Render Settings. The render settings will appear in the Inspector panel. There, notice the Skybox Material area. To apply a material here, either (1) drag a skybox material from the Project panel into the area where it says None (Material) or (2) click the little target icon at the far right of the Skybox Material line and pick a skybox material from the displayed swatches.
Figure 6.4 Skyboxes are constructed with materials made of six texture images. No GameObjects are involved.
Tips and Tricks
Either of these methods work. Sometimes, deciding which material to use is much easier to do by seeing a visual preview of it and other times, if the name is known, it's faster just to drag it from the Project panel. It's part of the power of Unity that either way will work.
Step 11: Try throwing a few different skybox materials in there to see the
dramatic difference it makes in the scene. Especially if your copy of Unity is Pro and the water is reflective, the entire color balance of a scene changes with a skybox (Figure 6.5).
Tips and Tricks
The built-in skyboxes are nice, but custom is always better. One of my favorite tools is to use C4D (which has some really great sky creation tools) and using the custom rig found here (http://forum.unity3d.com/ threads/9030-Unlimited-SkyBoxes-from-Cinema4D) to create and output the images needed to construct a custom skybox.
Step 12: Remove all skybox materials. Access the Render Settings
again (Edit>Render Settings) and click the target icon at the end of the Skybox Material line. Double-click the None swatch in the Select Material window.
Why?
Yes, I know—the skies were so pretty. Agreed—but they really are a bit too pretty and don't give the scene quite the ominous feeling this game requires. The other problem with a sky like that is the horizon must be taken care of in a reasonable way and this current model simply wasn't designed to do this. In the following steps, we will be making use of Unity's fog capabilities to help hide the end of the water and make the scene more ominous and interesting and (most importantly) appropriate for this genre of game.
Figure 6.5 Quick dramatic changes with various skyboxes.
Fog
In Unity, fog is handy and easy to use. Think of it as more than just the gray fog that is seen in the real world (although Unity's fog can be used to do that as well). Fog in Unity can be used to make dark scenes darker as the player gazes into the faraway corners of a space. Fog in Unity can be used to provide a soft haze on the horizon line to soften the sky/ground plane line. Fog can be used to make underwater feel like a murky mess, or just allow the water to “blue-out” as it gets further from the player. In short, fog—with a bit of tweaking—can be used for all sorts of interesting effects that can bring depth and polish to the scene.
In the case of Incursion, fog will be used to hide the edge of the water, and to give the scene a damp and murky feel. It will provide a sense of ambiance of a truly abandoned location, but one in which the player is never quite sure if he is alone.
Step 13: Change the background color of the Main Camera to gray.
The background color is an attribute of the Camera component. In the Hierarchy panel, expand the First Person Controller and select the Main Camera GameObject. Its attributes will appear in the Inspector. Under the Camera component, click the long blue swatch next to Background. Choose a new color that is rather gray-blue (this can be changed easily later; Figure 6.6). Jot down the RGBA values (mine were 89,101,120,255).
Why?
When there is no skybox present, cameras paint a solid color behind all visible geometry (or actually more accurately, it draws a solid color field and then draws the geometry on top of it). Unfortunately, fog doesn't affect this background quite as we would expect. Regardless of the density of the fog, that color field will still be visible. For that matter, a skybox is also still visible even if the fog is so thick the player can't see any other geometry. This, of course is not how fog really works, so to fix this, match the background color to the fog color to begin with so the fog really appears to thicken the further it gets away from the player even if there is no geometry out there.
Figure 6.6 Changing the background color.
Tips and Tricks
It's important to note that the background color is an attribute of a camera. This means that changes to the background color can only be seen through the camera. If Main Camera is selected in the Hierarchy, a little Camera Preview window will be set within the Scene window (Unity 3 or later only). Or, by bringing the Game window forward by clicking its tab, the background color changes will also become apparent. But, the background change will not be shown in the Scene window. Note that this also means different cameras can have different background colors. So if another camera is providing a map view or a night-vision view, that camera can have its own black (or whatever) color. Background color affects cameras, not the actual environment.
Step 14: Activate fog. Choose Edit>Render Settings (which will open in
the Inspector). Click the Fog check box.
Step 15: Change the Fog Color to match background. Still in the Render
Settings, the Fog Color has a long color swatch similar to the Background Color setting. Click this swatch and change the color to match the background (in my case 89, 101, 120, 255).
Step 16: Adjust the Fog Density. The higher the Fog Density value is, the
closer the fog obscures objects. This value can be adjusted in a couple of ways, either by clicking the numerical value and entering a new one or by moving the mouse up to the Fog Density word and click-dragging left or right (the cursor will have a double-tipped arrow appear (Figure 6.7). Adjust to about 0.04 (although change this to your taste).
Tips and Tricks
Generally, I find that dragging values can sometimes be a little tricky (it either slides way too high too quickly or never seems to move at all). A lot depends on the situation, but a quick way to work is to drag the value higher or lower to get in the general neighborhood of the value (and thus the look) that you are after. Then, for the fine-tuning, numerically tweak the value to taste.
Wrapping Up
The screenshot in Figure 6.8 shows the current results of water, fog, and background color adjustments made in this tutorial. Notice that the water may be a different color than yours. This comes from some tweaking made to the scene after all the previous steps were complete. You should do this too to make the water look as you'd like.
Tweak the background color to various hues to see which matches your vision of the scene. Be sure to match the fog to that value. The water has all sorts of parameters that can be tweaked. To tweak them, select the Daylight Water in the Hierarchy panel and make the adjustments in the Inspector panel.
Figure 6.8 shows water that had the Refraction Color adjusted to a dark green and the Wave Speed settings cut in half. These adjustments are fairly painless and can quickly help establish a different scale to the water, and an entirely different hue and style.
So there are still some issues here. The lighting—or lack of lighting— combined with a high ambient color setting still makes the scene look flat. However, a bit of reflective water and a splash of fog can go a long way to give a scene just a bit of depth.
Some other benefits of the approach here is that what was once a very small space (just the dock) suddenly feels like a much bigger space. The player can feel the large space of the sea out beyond the dock even if he cannot see it. Although in the next few tutorials the hill this base is built into will be built, not much of it will be seen, although it will be implied. Very often, the most expansive-feeling levels may not be that large at all.