Hour 6. Game 1: Amazing Racer

What You’ll Learn in This Hour:

How to design a basic game

How to apply your knowledge of terrains to build a game-specific world

How to add objects to a game to provide interactivity

How to playtest and tweak a finished game

In this hour, you’ll use what you have learned so far to build your first Unity game. This lesson starts by covering the basic design elements of the game. From there, you will build the world in which the game will take place. Then you’ll add some interactivity objects to make the game playable. You’ll finish by playing the game and making any necessary tweaks to improve the experience.

Design

The design portion of game development is where you plan ahead of time all the major features and components of a game. You can think of it as laying down the blueprint so that the actual construction process is smoother. When making a game, a lot of time is normally spent working through the design. Because the game you are making in this hour is fairly basic, the design phase will go quickly. You need to focus on three areas of planning to make this game: the concept, the rules, and the requirements.

The Concept

The idea behind this game is simple: You start at one end of an area and run quickly to the other side. There are hills, trees, and obstacles in your path. Your goal is to see how fast you can make it to the finish zone. This game concept was chosen for your first game because it highlights everything you have worked on so far in this book. Also, because you have not learned scripting in Unity yet, you cannot add very elaborate interactions. Games you make in later hours will be more complex.

The Rules

Every game must have a set of rules. The rules serve two purposes. First, they say how the player will actually play the game. Second, because software is a process of permission (see the “Process of Permission” note), the rules dictate the actions available to the players to overcome challenges. The rules for Amazing Racer are as follows:

There is no win or loss condition, only a completed condition. The game is completed when the player enters the finish zone.

The player always spawns in the same spot. The finish zone is always in the same spot.

There are water hazards, and whenever the player falls into one of them, the player is moved back to the spawn point.

The objective of the game is to try to get the fastest time possible. This is an implicit rule and is not specifically built into the game. Instead, cues are built into the game as hints to the player that this is the goal. The idea is that the players will intuit the desire for a faster time based on the signals given to them.

The Requirements

An important step in the design process is determining which assets will be required for the game. Generally speaking, a game development team is made up of several individuals. Some of them handle designing, others work with programming, and others make art. Every member of the team needs something to do to be productive during every step of the development process. If everyone waited until something was needed to begin working, there would be a lot of starting and stopping. Instead, you determine your assets ahead of time so that things can be created before they are needed. Here is a list of all the requirements for Amazing Racer:

A piece of rectangular terrain. The terrain needs to be big enough to present a challenging race. The terrain should have obstacles built in as well as a designated spawn and finish point (see Figure 6.1).

Textures and environment effects for the terrain. These are provided in the Unity standard assets.

A spawn point object, a finish zone object, and a water hazard object. These will be generated in Unity.

A character controller. This is provided by the Unity standard assets.

A graphical user interface (GUI). This will be provided for you in the book assets. Note that this hour uses the old-style GUI, which works purely from script, for simplicity. In your projects, use the new UI system introduced in Hour 14, “User Interfaces.”

A game manager. This will be created in Unity.

Images

FIGURE 6.1
The general terrain layout for the game Amazing Racer..

Creating the Game World

Now that you have a basic idea of the game, it is time to start building it. There are many places to begin building a game. For this project, you’ll begin with the world. Because this is a linear racing game, the world will be longer than it is wide (or wider than it is long, depending on how you look at it). You will use many of the Unity standard assets to rapidly create the game.

Sculpting the World

There are many ways you can create the terrain for Amazing Racer. Everyone will probably have a different vision for it in his or her head. To streamline the process and ensure that everyone will have the same experiences during this hour, a heightmap has been provided for you. Be sure to refer back to Hour 4 if you get stuck working with the terrain. To sculpt the terrain, follow these steps:

1. Create a new project and name it Amazing Racer. Add a terrain to the project and position its transform at (0, 0, 0) in the Inspector.

2. Locate the file TerrainHeightmap.raw in the book assets for Hour 6. Import the TerrainHeightmap.raw file as a heightmap for the terrain (by clicking Import Raw in the Texture Resolutions section under Terrain Settings in the Inspector).

3. In the Import Heightmap dialog window change the Byte Order property to Mac and set Terrain Size to 200 wide (X) by 100 tall (Y) by 200 long (Z).

4. Save the current scene as Main.

The terrain should now be sculpted to match the world in the book. Feel free to make minor tweaks and changes to your liking.

Adding the Environment

At this point, you can begin texturing and adding the environment effects to your terrain. You need to import the Environment assets. Locate the EnvironmentAssets.unitypackage file in the Hour 6 book files and drag it into your project.

You now have a bit of freedom to decorate the world however you would like. The suggestions in the following steps are guidelines. Feel free to do things in a manner that looks good to you:

1. Rotate the directional light to suit your preference.

2. Texture the terrain. The sample project uses the following textures: GrassHillAlbedo for flat parts, CliffAlbedoSpecular for steep parts, GrassRockyAlbedo for the areas in between, and MudRockyAlbedoSpecular for inside the pits.

3. Add trees to your terrain. Trees should be placed sparsely and mostly on flat surfaces.

4. Add some water to your scene from the Environment assets. Locate the Water4Advanced prefab in the folder AssetsEnvironmentWaterWater4Prefabs and drag it into your scene. (You’ll learn more info about prefabs in Hour 11, “Prefabs.”) Position the water at (100, 29, 100) and scale it to (2, 1, 2).

The terrain should now be prepared and ready to go. Be sure to spend a good amount of time on texturing to make sure you have a good blend and a realistic look.

Fog

In Unity, you can add fog to a scene to simulate many different natural occurrences, such as haze, actual fog, or the fading of objects over great distances. You can also use fog to give new and alien appearances to your world. In the Amazing Racer game, you can use fog to obscure the distant parts of the terrain and add an element of exploration.

Adding fog is quite simple:

1. Select Window > Rendering > Lighting. The Lighting window opens.

2. Select the Environment tab and turn on fog by checking the Fog check box under Other Settings.

3. Change the color to white and set the density to 0.005. (Note: These are arbitrary values and can be changed (or omitted) based on your preferences.)

4. Experiment with the different fog densities and colors. Table 6.1 describes the various fog properties.

Several properties impact how fog looks in a scene. Table 6.1 describes these properties.

TABLE 6.1 Fog Properties

Images

Skyboxes

You can add some punch to a game by adding a skybox. A skybox is a large box that goes around a world. Even though it is a cube consisting of six flat sides, it has inward-facing textures to make it look round and infinite. You can create your own skyboxes or use Unity’s standard skybox, which comes enabled in every 3D scene. For the most part, in this book, you will use the built-in ones.

The standard skybox is called a “procedural skybox.” This means the color is not fixed but is instead calculated and can change. You can see this by rotating your scene’s directional light. Notice how the color of the sky, and even the simulated sun, changes as the light is rotated. Procedural skyboxes key off a scene’s main directional light by default.

Creating and applying your own custom procedural skybox is quite simple:

1. Right-click the Project view and select Create > Material. (Skyboxes are really just materials applied to a “giant box in the sky.”)

2. In the Inspector for this new material, click the Shader drop-down and select Skybox > Procedural. Note that this is where you can also choose to create 6 Sided, Cubemap, or Panoramic skyboxes.

3. Apply the skybox to the scene through the Lighting settings window (which you open by selecting Window > Rendering > Lighting > Environment). Alternatively, you can just drag your skybox material into any empty space in the scene view. When you apply the skybox to your scene, you don’t immediately see any change. The skybox you just created has the same properties as the default skybox and thus will look exactly the same.

4. Experiment with the different skybox properties. You can modify how the sun looks, experiment with how light scatters in the atmosphere, and change the sky color and exposure. Feel free to make something truly alien and unique for your game.

Skyboxes don’t have to be procedural. They can use six textures to create highly detailed skies (often called cubemaps). They could even contain HDR or panorama images. All these settings are available depending on the type of skybox shader you choose. For the most part, though, you will be working with procedural skyboxes in this book because they are easy to get up and running quickly.

The Character Controller

At this stage of development, add a character controller to your terrain:

1. Import the character controller by locating the Hour 6 book file FPSController_NoPhysics.unitypackage and dragging it into your project.

2. Locate the FPSController asset in the folder AssetsFirstPersonCharacter and drag it into your scene.

3. Position the controller (which is named FPSController and is blue in the Hierarchy view) at (165, 32, 125). If the controller doesn’t seem to be positioned correctly on the terrain, ensure that the terrain is positioned at (0, 0, 0), as per the previous exercise. Now rotate the controller to 260 on the y axis so that it faces the correct direction. Rename the controller object Player.

4. Experiment with the First Person Controller and Character Controller components on the Player game object. These two components control much of how the player will be able to behave in your game. For instance, if your character is able to climb over hills that you want to be impassable, you could lower the Slope Limit property on the Character Controller component.

5. Because the Player controller has its own camera, delete the Main Camera from the scene.

Once the character controller is in your scene and positioned, play the scene. Be sure to move around and look for any areas that need to be fixed or smoothed. Pay attention to the borders. Look for any areas where you are able to escape the world. Those places need to be raised, or the controller needs to be modified, so that the player cannot fall off the map. This is the stage at which you generally fix any basic problems with your terrain.

Gamification

You now have a world in which your game can take place. You can run around and experience the world to an extent. The piece that is missing is the game itself. Right now, what you have is considered a toy. It is something that you can play with. What you want is a game, which is a toy that has rules and a goal. The process of turning something into a game is called gamification, and that’s what this section is all about. If you followed the previous steps, your game project should now look something like Figure 6.2 (though your choices for fog, skybox, and vegetation may create some differences). The next few steps are to add game control objects for interaction, apply game scripts to those objects, and connect them to each other.

Images

FIGURE 6.2
The current state of the Amazing Racer game.

Adding Game Control Objects

As defined earlier in this hour, in the section “The Requirements,” you need four specific game control objects. The first object is a spawn point—a simple game object that exists solely to tell the game where to spawn the player. To create the spawn point, follow these steps:

1. Add an empty game object to the scene (by selecting GameObject > Create Empty).

2. Position the game object at (165, 32, 125) and give it a rotation of (0, 260, 0).

3. Rename the empty object Spawn Point in the Hierarchy view.

Next, you need to create the water hazard detector. This will be a simple plane that will sit just below the water. The plane will have a trigger collider (as covered in more detail in Hour 9, “Collision”), which will detect when a player has fallen in the water. To create the detector, follow these steps:

1. Add a plane to the scene (by selecting GameObject > 3D Object > Plane) and position it at (100, 27, 100). Scale the plane to (20, 1, 20).

2. Rename the plane Water Hazard Detector in the Hierarchy view.

3. Check the Convex and Is Trigger check boxes on the Mesh Collider component in the Inspector view (see Figure 6.3).

4. Make the object invisible by disabling its Mesh Renderer component. Do this by unchecking the box next to the Mesh Renderer component’s name in the Inspector (see Figure 6.3).

Images

FIGURE 6.3
The Inspector view of the Water Hazard Detector object.

Next, you need to add the finish zone to your game. This zone will be a simple object with a point light on it so that the player knows where to go. The object will have a capsule collider attached to it so that it will know when a player enters the zone. To add the Finish Zone object, follow these steps:

1. Add an empty game object to the scene and position it at (26, 32, 37).

2. Rename the object Finish Zone in the Hierarchy view.

3. Add a light component to the Finish Zone object. (With the object selected, click Component > Rendering > Light.) Change the type to Point if it isn’t already set to this and set the range to 35 and intensity to 3.

4. Add a capsule collider to the Finish Zone object by selecting the object and clicking Component > Physics > Capsule Collider. Check the Is Trigger check box and change the Radius property to 9 in the Inspector view (see Figure 6.4).

Images

FIGURE 6.4
The Inspector view of the Finish Zone object.

The final object you need to create is the Game Manager object. This object doesn’t technically need to exist. You could instead just apply its properties to some other persistent object in the game world, such as the Main Camera. You generally create a dedicated game manager object to prevent any accidental deletion, though. During this phase of development, the game manager object is very basic. It will be used more later on. To create the Game Manager object, follow these steps:

1. Add an empty game object to the scene.

2. Rename the game object Game Manager in the Hierarchy view.

Adding Scripts

As mentioned earlier, scripts specify behaviors for game objects. In this section, you’ll apply scripts to your game objects. At this point, it is not important for you to understand what these scripts do. You can add scripts to your project in one of two ways:

Drag existing scripts into the Project view of your project.

Create new scripts in your project by right-clicking in the Project view and selecting Create > C# Script.

Once scripts are in a project, applying them is easy. To apply a script, simply drag it from the Project view onto whatever object you want to apply it to in either the Hierarchy view or the Inspector view (see Figure 6.5).

Images

FIGURE 6.5
Applying scripts by dragging them onto game objects.

You could also apply a script by dragging it onto an object in the Scene view, but if you do, you run the risk of missing and accidently putting the script on some other object. Therefore, applying scripts through the Scene view is not advisable.

Try it Yourself

Importing and Attaching Scripts

Follow these steps to import the scripts from the book files and attach them to the correct objects:

1. Create a new folder in your Project view and name it Scripts. Locate the three scripts in the Hour 6 book files—FinishZone.cs, GameManager.cs, and PlayerRespawn.cs—and drag them into your newly created Scripts folder.

2. Drag the FinishZone.cs script from the Project view onto the Finish Zone game object in the Hierarchy view.

3. Select the Game Manager object in the hierarchy. In the Inspector, select Add Component > Scripts > GameManager. (This is an alternative way of adding a script component to a game object.)

4. Drag the PlayerRespawn.cs script from the Project view onto the Water Hazard Detector game object in the hierarchy.

Connecting the Scripts

If you read through the scripts earlier, you may have noticed that they all have placeholders for other objects. These placeholders allow one script to talk to another script. For every placeholder in these scripts, there is a property in the component for that script in the Inspector view. Just as with scripts, you apply the objects to the placeholders by clicking and dragging (see Figure 6.6).

Images

FIGURE 6.6
Moving game objects onto placeholders.

Try it Yourself

Connecting Scripts to Scripts

Follow these steps to give the scripts the game objects they need to function correctly:

1. Select the Water Hazard Detector object in the Hierarchy view. Notice that the Player Respawn component has a Game Manager property. This property is a placeholder for the Game Manager object you made previously.

2. Click and drag the Game Manager object from the Hierarchy view onto the Game Manager property of the Player Respawn (Script) component. Now, whenever players fall into the water hazard, the water hazard will let the Game Manager object know, and the player will get moved back to the spawn point at the beginning of the level.

3. Select the Finish Zone game object. Click and drag the Game Manager object from the Hierarchy view onto the Game Manager property of the Finish Zone (Script) component in the Inspector view. Now, whenever the player enters the finish zone, the game control will be notified.

4. Select the Game Manager object. Click and drag the Spawn Point object onto the Spawn Point property of the Game Manager (Script) component.

5. Click and drag the Player object (this is the character controller) onto the Player property of the Game Manager object.

That’s all there is to connecting the game objects. Your game is now completely playable! Some of this might not make sense right now, but the more you study it and work with it, the more intuitive it becomes.

Playtesting

Your game is now done, but it is not time to rest just yet. Now you have to begin the process of playtesting. Playtesting involves playing a game with the intention of finding errors or things that just aren’t as fun as you thought they would be. A lot of times, it can be beneficial to have other people playtest your games so that they can tell you what makes sense to them and what they found enjoyable.

If you followed all the steps previously described, there shouldn’t be any errors (commonly called bugs) for you to find—at least, I hope so. The process of determining what parts are fun, however, is completely at the discretion of the person making the game. Therefore, this part is left up to you. Play the game and see what you don’t like. Take notes of the things that aren’t enjoyable to you. Don’t just focus on the negative, though. Also find the things that you like. Your ability to change these things may be limited at the moment, so write them down. Plan on how you would change the game if you had the opportunity.

One simple thing you can tweak right now to make the game more enjoyable is the player’s speed. If you have played the game a couple of times, you might have noticed that the character moves rather slowly, and that can make the game feel very long and drawn out. To make the character fast, you need to modify the First Person Controller (Script) component on the Player object. In the Inspector view change the run speed (see Figure 6.7). The sample project has this set at 10. Try faster or slower speeds and pick one you enjoy. (You did notice that holding down Shift as you play makes you run, right?)

Images

FIGURE 6.7
Changing the player’s walk and run speeds.

Summary

In this hour, you made your first game in Unity. You started by looking at the various aspects of the game’s concept, rules, and requirements. From there, you built the game world and added environment effects. Then you added the game objects required for interactivity. You applied scripts to those game objects and connected them. Finally, you playtested your game and noted the things you liked and didn’t like.

Q&A

Q. This lesson seems over my head. Am I doing something wrong?

A. Not at all! This process can feel very alien to someone who is not used to it. Keep reading and studying the materials, and it will all begin to come together. The best thing you can do is pay attention to how the objects connect through the scripts.

Q. You didn’t cover how to build and deploy the game. Why not?

A. Building and deployment are covered later in this book, in Hour 23, “Polish and Deploy.” There are many things to consider when building a game, and at this point, you should just focus on the concepts required to develop it. If you really can’t wait, feel free to sneak a peak at Hour 23 a bit early. I promise I won’t tell!

Q. Why couldn’t I make a game without scripts?

A. As mentioned earlier, scripts define the behavior of objects. It is very difficult to have a coherent game without some form of interactive behavior. The only reason you are building a game in Hour 6 before learning scripting in Hours 8 and 9 is to reinforce the topics you have already learned before moving on to something different.

Workshop

Take some time to work through the questions here to ensure that you have a firm grasp of the material.

Quiz

1. What are a game’s requirements?

2. What is the win condition of the Amazing Racer game?

3. Which object is responsible for controlling the flow of the game?

4. Why is it important to playtest a game?

Answers

1. The requirements are the list of assets that will need to be created to make the game.

2. Trick question! There is no explicit win condition for this game. It is assumed that the player wins when he or she gets a better time than previous attempts. This is not built into the game in any way, though.

3. The game manager, which in the Amazing Racer game is called Game Manager

4. To discover bugs and determine what parts of the game work the way you want them to

Exercise

The best part about making games is that you get to make them the way you want. Following a guide can be a good learning experience, but you don’t get the satisfaction of making a custom game. For this exercise, modify the Amazing Racer game a little to make it more unique. Exactly how you change the game is up to you. Some suggestions are listed here:

Try to add multiple finish zones. See whether you can place them in a way that offers the players more choices.

Modify the terrain to have more or different hazards. As long as the hazards are built like the water hazard (including the script), they will work just fine.

Try having multiple spawn locations. Make it so some of the hazards move the player to a different spawn point.

Modify the sky and textures to create an alien world. Make the world experience unique.

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

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