Technology is a tool. It helps us accomplish amazing things, hopefully more quickly, more easily, and more amazingly if we hadn't used the tool. Before we had newfangled steam-powered hammering machines, we had hammers. And before we had hammers, we had the painful process of smacking a nail into a board with our bare hands. Technology is all about making our lives better, easier, and less painful.
Unity 3D is a relatively new piece of technology that strives to make life better and easier for game developers. Unity is a game engine or a game authoring tool that enables creative folk like you to build video games.
By using Unity, you can build video games more quickly and easily than ever before. In the past, building games required an enormous stack of punch cards, a computer that filled a whole room, and a burnt sacrificial offering to an ancient God named Fortran. Today, instead of spanking nails into boards with your palm, you have Unity. Consider it your hammer a new piece of technology for your creative tool belt.
Throughout this book, we'll be distilling our game development dreams down to small, bite-sized nuggets instead of launching into any sweepingly epic open-world games. The idea here is to focus on something you can actually finish instead of getting bogged down in an impossibly ambitious opus. This book will teach you to build four games, each of which focus on a small, simple gameplay mechanic. You'll learn how to build discrete pieces of functionality that you can apply to each project, filling the games out to make them complete experiences. When you're finished, you can publish these games on the Web, a Mac, or a PC.
The team behind Unity 3D is constantly working on packages and export options for other platforms. At the time of writing, Unity could additionally create games that can be played on the iPhone, iPod, iPad, Android devices, Xbox Live Arcade, PS3, and Nintendo's WiiWare service. Each of these tools is add-on functionality to the core Unity package, and comes at an additional cost. As we're focusing on what we can do without breaking the bank, we'll stick to the core Unity 3D program for the remainder of this book.
With the initial skills that you learn in this book, you'll be able to expand your knowledge and start building more and more complex projects. The key is to start with something you can finish, and then for each new project that you build, to add small pieces of functionality that challenge you and expand your knowledge. Any successful plan for world domination begins by drawing a territorial border in your backyard; consider this book your backyard.
One of Unity's most astonishing capabilities is that it can deliver a full 3D game experience right inside your web browser. It does this with the Unity Web Player a free plugin that embeds and runs Unity content on the Web.
Before you dive into the world of Unity games, download the Unity Web Player. In much the same way that Flash player runs Flash-created content, the Unity Web Player is a plugin that runs Unity-created content in your web browser.
Go to http://unity3D.com.
Click on the button on the main page to install the Unity Web Player.
Follow all of the onscreen prompts until the Web Player has finished installing.
The process is only slightly more involved on a Mac. You have to download and run a
.dmgfile, and then enter your administrator password to install the plugin, but it's relatively quick and painless.
Now that you've installed the Web Player, you can view the content created with the Unity 3D authoring tool in your browser.
In order to fully appreciate how fancy this new hammer is, let's take a look at some projects that other people have created with Unity. While these games may be completely out of our reach at the moment, let's find out how game developers have pushed this amazing tool to its very limits.
The first stop on our whirlwind Unity tour is FusionFall a Massively Multiplayer Online Role-Playing Game (MMORPG). You can find it at fusionfall.com. You may need to register to play, but it's definitely worth the extra effort!
FusionFall was commissioned by the Cartoon Network television franchise, and takes place in a re-imagined, anime-style world where popular Cartoon Network characters are all grown up. Darker and more sophisticated versions of the Powerpuff Girls, Dexter, Foster and his imaginary friends, and the kids from Codename: Kids Next Door run around battling a slimy green alien menace.
FusionFall is a very big and very expensive high-profile game that helped draw a lot of attention to the then-unknown Unity game engine when the game was released. As a tech demo, it's one of the very best showcases of what your new technological hammer can really do! FusionFall has real-time multiplayer networking, chat, quests, combat, inventory, NPCs (non-player characters), basic AI (artificial intelligence), name generation, avatar creation, and costumes. And that's just a highlight of the game's feature set. This game packs a lot of depth.
Unfortunately, a step-by-step guide in creating a game of the size and scope of FusionFall would likely require its own flatbed truck to transport, and you'd need a few friends to help you turn each enormous page. It would take you the rest of your life to read, and on your deathbed, you'd finally realize the grave error that you had made in ordering it online in the first place, despite having qualified for free shipping.
Here's why: check out the game credits link on the Fusion http://fusionfall.cartoonnetwork.com/game/credits.php.
This page lists all of the people involved in bringing the game to life. Cartoon Network enlisted the help of an experienced Korean MMO developer called Grigon Entertainment. There are over 80 names on that credits list! Clearly, only two courses of action are available to you:
Build a cloning machine and make 79 copies of yourself. Send each of those copies to school to study various disciplines, including marketing, server programming, and 3D animation. Then spend a year building the game with your clones. Keep track of who's who by using a sophisticated armband system.
Give up now because you'll never make the game of your dreams.
Before you do something rash and abandon game development for farming, let's take another look at this. FusionFall is very impressive, and it might look a lot like the game that you've always dreamed of making. This book is not about crushing your dreams. It's about dialing down your expectations, putting those dreams in an airtight jar, and taking baby steps. Confucius said: "A journey of a thousand miles begins with a single step." I don't know much about the man's hobbies, but if he was into video games, he might have said something similar about them creating a game with a thousand awesome features begins by creating a single, less feature-rich game.
So, let's put the FusionFall dream in an airtight jar and come back to it when we're ready. We'll take a look at some smaller Unity 3D game examples and talk about what it took to build them.
No tour of Unity 3D games would be complete without a trip to Blurst.com the game portal owned and operated by indie game developer Flashbang Studios. In addition to hosting games by other indie game developers, Flashbang has packed Blurst with its own slate of kooky content, including Off-Road Velociraptor Safari.
In Off-Road Velociraptor Safari, you play a dinosaur in a pith helmet and a monocle driving a jeep equipped with a deadly spiked ball on a chain (just like in the archaeology textbooks). Your goal is to spin around in your jeep doing tricks and murdering your fellow dinosaurs (obviously).
For many indie game developers and reviewers, Off-Road Velociraptor Safari was their first introduction to Unity. Some reviewers said that they were stunned that a fully 3D game could play in the browser. Other reviewers were a little bummed that the game was sluggish on slower computers. We'll talk about optimization a little later, but it's not too early to keep performance in mind as you start out.
If you play Off-Road Velociraptor Safari and some of the other games on the Blurst site, you'll get a better sense of what you can do with Unity without a team of experienced Korean MMO developers. The game has 3D models, physics (code that controls how things move around somewhat realistically), collisions (code that detects when things hit each other), music, and sound effects. Just like FusionFall, the game can be played in the browser with the Unity Web Player plugin. Flashbang Studios also sells downloadable versions of its games, demonstrating that Unity can produce standalone executable game files too.
Right then! We can't create FusionFall just yet, but we can surely create a tiny game like Off-Road Velociraptor Safari, right? Well... no. Again, this book isn't about crushing your game development dreams. But the fact remains that Off-Road Velociraptor Safari took five supremely talented and experienced guys eight weeks to build on full-time hours, and they've been tweaking and improving it ever since. Even a game like this, which may seem quite small in comparison to a full-blown MMO game like FusionFall, is a daunting challenge for a solo developer. Put it in a jar up on the shelf, and let's take a look at something you'll have more success with.
Wooglie.com is a Unity game portal hosted by M2H Game Studio in the Netherlands. One glance at the front page will tell you that it's a far different portal than Blurst.com. Many of the Wooglie games are rough around the edges, and lack the sophistication and the slick professional sheen of the games on Blurst. But here is where we'll make our start with Unity. This is exactly where you need to begin as a new game developer, or as someone approaching a new piece of technology like Unity.
Play through a selection of games on Wooglie. I'll highlight a few of them for your interest:
Big Fun Racing is a simple but effective game where you zip around collecting coins in a toy truck. It features a number of different levels and unlockable vehicles. The game designer sunk a few months into the game in his off-hours; with a little help from outsource artists to create the vehicle models.
Diceworks is a very simple, well-polished game designed for the iPhone in Unity 3D. We won't be covering any iPhone development, but it's good to know that your Unity content can be deployed to a number of other devices and platforms, including Apple iOS, Android, and the Nintendo Wii. These add-on versions of the software cost an additional fee, but you can deploy your games to the Web, to the Mac, and to your PC for free using the indie version of Unity.
Diceworks was created by one artist and one programmer working together as a team. It's rare to find a single person who possesses both programming and artistic talent simultaneously; scientists say that these disciplines are split between two different lobes in our brains, and we tend to favor one or the other. The artist-programmer pairing that produced Diceworks is a common setup in game development. What's your own brain telling you? Are you more comfy with visuals or logic? Art or programming? Once you discover the answer, it's not a bad plan to find someone to make up the other half of your brain so that your game handles both areas competently.
At any event, with Diceworks we're definitely getting closer to the scope and scale that you can manage on your own as you start out with Unity.
It's also interesting to note that Diceworks is a 2D game created in a 3D engine. The third "D" is largely missing, and all of the game elements appear to exist on a flat plane. Nixing that extra dimension when you're just starting out isn't a half bad idea. Adding depth to your game brings a whole new dimension of difficulty to your designs, and it will be easier to get up and running with Unity by focusing on the X and Y axes, and leaving the Z-axis in one of those dream jars. With a few sturdy working game examples under your belt, it won't be long before you can take that jar with Z axis down off the shelf and pop it open. The games that we'll be building in this book will stick to a two-dimensional plane, using three-dimensional models. Even so, certain games have taken this concept and ran with it. For example, the New Super Mario Bros. Wii locked its 3D characters to a 2D plane and wound up an extremely complex and satisfying platformer.
A common mistake that new game developers make is biting off more than they can chew. Even experienced game developers make this mistake when they get really excited about a project, or when they approach a new technology and expect to be immediately proficient at using it. The real danger here is that you'll sit down and try to create your dream let's say it's a sword and sorcery RPG epic that combines all the best parts of Diablo, ChuChu Rocket!, and Microsoft Excel. When you've sunk days and weeks and months into it and it still looks nothing like the game you envisioned, you give up. You figure that since you failed at creating your dream game, you were never really cut out to be a game developer to begin with.
You owe it to yourself to start small! Rome wasn't built in a day, and neither was your dream kart racing game starring famous figures from Roman history. By taking smaller steps, you can experience success with a number of smaller games. Then you can take what you learn and add to it, slowly building your expertise until you're in a position to take that dream game jar off the shelf.
For now, let's keep our dream shelf fully stocked, and turn our attention to something small and achievable. By the end of this book, you'll have a collection of working games that started out simply, and grew more and more complex as you got smarter. My hope is that once you finish the book, you'll be well-equipped to dream up new incremental features for your games, and to hunt down the resources you need to fill the gaps in your new-found knowledge.
In Chapter 2, we'll go into detail about where you should start when you're deciding what kind of game to create. We'll also see some real-world examples of games that began as simple, effective ideas and later grew into enormously complex and feature-rich titles. From small acorns, mighty multiplayer oak tree games grow.
We'll be learning a lot about iteration throughout this book. Some game developers who produce content for fixed media such as game disks and cartridges are used to producing a gold master the final build of the game and calling it a day. One of the joys of deploying games to the Web is that they're never truly finished. You can continue tweaking your web games and modifying them until you end up with a far more fun and polished game than you started with.
Flashbang Studios constantly modified and improved upon Off-Road Velociraptor Safari even years after they were "finished" - three years afterward, in fact! The team addressed critics' initial concerns about sluggish performance by relentlessly tweaking the game and improving upon its performance.
Likewise, we'll be creating some games that are really raw and unfinished at first. But as we learn more about how to program the crucial bits and pieces common to many games, we'll keep revisiting our rough, early games to add those pieces and improve them.
Now that you've seen some of what Unity can do, it's time to download the program and kick the tires! Unity indie version is available for the low price of free (at the time of writing) from the Unity 3D website.
Go to http://unity3D.com.
Click on the Download Now button.
Download the latest version of the Unity 3D authoring tool for your platform Mac or PC. If you are given the option, make sure to download the sample project along with the program.
Follow all the onscreen prompts until the Unity authoring tool has finished installing.
After a quick registration process, Unity is ready to go. With any luck, the AngryBots Demo will automatically open. If it doesn't, and you're faced with a dialog asking you to open a project, you can find the AngryBots Demo here by default:
C:\Documents and Settings\All Users\Documents\Unity Projects\AngryBots
C:\Documents and Settings\All Users\Shared Documents\Unity Projects\AngryBots
If you thought you'd be a rebel and you unchecked the sample projects box when you downloaded Unity, you may find yourself re-downloading Unity to get the AngryBots Demo. You can pull down other sample learning projects such as the AngryBots Demo from the Unity website:
As the Unity Technologies team improves the software, they launch new and more impressive demos to show off what Unity can do. If you're reading this book, and your copy of Unity 3D launches a different demo project, don't freak out everything we're about to discuss can be generally applied to most demos.
When the AngryBots Demo first opens, you should see a splash screen referring you to different tutorial resources and language guides. How helpful! Now close it. (Don't worry; it'll be there next time, unless you uncheck the Show at Startup checkbox). If you checked the box but you'd really like to see that Welcome Screen again, look in the menus under Help | Welcome Screen.
Go to Window | Layouts | 2 by 3 menu to see the different panels that we are about to tour.
To try out the demo, click on the Play button at the top-center of the screen.
You can walk around the AngryBots Demo using the WASD keys on your keyboard. Hold down the main mouse button to fire your boomstick at the aggravated automatons. When you're finished exploring, press the Esc key to pause the game and regain mouse control. Then click on the Play button again to end the demo.
Unity contains terrain tools that let you model your level right inside the software. It contains a readymade First Person Controller Prefab object you can plunk into the world with automatic WASD keyboard controls that will allow you to explore the terrain, or you can replace the AngryBots hero with your own character to build a third-person game. Unity automatically takes care of the rendering (drawing), collisions, physics, and sound effects. That's one fancy hammer!
Wide-open worlds with Will
If you'd like to learn how to sculpt your own terrain in Unity, and to add 3D models, sounds, and interactivity to create a simple but functional 3D open-world game, check out, Unity 3.x Game Development Essentials, Will Goldstone, Packt Publishing.
Much of what you see in the AngryBots Demo can't be built directly in Unity. Most of the assets were created with other software; Unity is the program you use to put everything together and to make it interactive. The demo contains special models, such as the airlocks, which were imported from 3D software packages like 3D Studio Max, Maya, or Blender. Certain elements, such as robot enemies, have scripts attached to them. Scripts are lists of instructions that tell the items in the game world how to behave. Throughout the book, we'll learn how to import 3D models and to write scripts to control them.
Let's take a quick look around the Unity interface and note a few points of interest.
The Scene window is where you can position your Game Objects and move them around. This window has various controls to change its level of detail. Use these controls to toggle lighting on and off, and to display the window contents with textures, wireframes, or a combination of both. You can use the colorful gizmo in the top-right corner to constrain the view to the X, Y, and Z axes to view the top and sides of your scene. Click on the white box in the middle to return to perspective view. This window also features a search field. Try clicking on the gizmo's green Y cone to view the AngryBots Demo from above, and then type rock into the search field. Every object with "rock" in its name lights up, while the rest of the scene fades to grayscale. Press the tiny x button to clear the search field.
The Game window shows you what your players will see. When you click on the Play button to test your game (as you just did with the AngryBots Demo) the results of your efforts play out in this window. Toggle the Maximize on Play button to test your game in full-screen mode.
The Hierarchy panel lists all of the Game Objects in your Scene. Game Objects cameras, lights, models, and prefabs are the things that make up your game. They can be "tangible" things like the airlock doors and the cannisters in the AngryBots Demo. They can also include intangible things, which only you as the game developer get to see and play with, such as the cameras, the lights, and colliders, which are special invisible shapes that tell the game engine when two Game Objects are touching.
The AngryBots Demo Hierarchy contains Game Objects for the cannisters, the tables, the airlocks and the computer terminals, to name a few. It also lists the Player, a very complicated Game Object that controls how the hero moves and collides with his environment. The player character has a camera following him. That camera is our eye into the game world. The demo lists a collection called Environment (sounds) a series of Game Objects that determine what the player hears when he walks through different parts of the level (such as torrential rain outside, and the droning equipment hum when he moves indoors). So, Game Objects can include touchy-feely "physical" objects such as cannisters and airlocks, as well as behind-the-scenes intangible things such as lights, cameras, and actions (scripts).
Click on a Game Object in the Hierarchy panel, and then hover your mouse over the Scene window. Press the F key on your keyboard, and the Scene window will automatically pan and zoom directly to that object. Alternatively, you can go to Edit | Frame Selected, which can be more reliable than using the keyboard shortcut. (I like to think of the F as standing for Focus to help me remember what this shortcut does.)
The Project panel lists all of the elements that you'll use to create Game Objects in your project. For example, look for the
mech_bot in the
Objects/Enemies folder. The AngryBots Demo EnemyMech Game Object is made up of a series of meshes that represent the mech's shape, a material to depict its "skin" or coloring, and an animation to describe its movement. All of these types of goodies are listed in the Project panel.
The Project panel displays the contents of a special folder on your computer's operating system called
Assets. Unity automatically creates the
Assets folder for you when you create a new project. If you drag a compatible file, like a 3D model, a sound effect, or an image into the Project panel, Unity copies it to the
Assets folder behind the scenes, and displays it in the Project panel.
The Inspector is a context-sensitive panel, which means that it changes depending on what you select elsewhere in Unity. This is where you can adjust the position, rotation, and scale of Game Objects listed in the Hierarchy panel. The Inspector can also display controls to configure components that add functionality to Game Objects. Between the three main panels in Unity (Hierarchy, Project, and Inspector), the Inspector is where you'll likely spend most of your time because that's where you'll be tweaking and fiddling with every aspect of the elements that comprise your game projects.
The preceding screenshot of the Inspector shows the components attached to the Player Game Object in the AngryBots Demo: a number of scripts (including Free Movement Motor and Player Move Controller), a Rigidbody component, a Capsule Collider, and others. To see the same content on your computer, click to open the Player Game Object in the Hierarchy panel.
Let's use the Inspector panel to make a quick change to the orientation of the character. We'll begin the demo with the hero standing on his head (which is a sure-fire way to make those bots even angrier, by the way).
We can use the Inspector panel to change the rotation of the player. Follow these steps:
In the Hierarchy panel, click to select the Player Game Object.
Click on the Rotate button, which looks like two arrows sniffing each others' behinds.
A globe appears around the bottom of the Player Game Object. The red X-axis rotator handle encircles the player's body. Clicking and dragging it rotates the player model as if he were standing in a very dodgy canoe. The blue Z-axis rotator handle rotates the player as if there was an invisible pin running through his ankles. If we click-and-drag that handle, the player rotates to either fall flat on his face, or flat on his back, like he's got space sickness. And the green Y-axis rotator handle runs around the player like a hula hoop.
Dragging this handle around makes the player spin to face different directions. The Player Game Object can get pretty hairy; in order to isolate the rotation controls, type player into the Scene window's search field to exclude all other Game Objects. (Remember that you can bring the selected Game Object into view by pressing the F key when your mouse cursor is within the Scene view.)
You can click-and-drag the red X-axis arrow to turn the player upside down, but a better method is to change the X-axis rotation in the Inspector panel. Expand the gray arrow next to Transform in the Inspector panel if it's not already open, and change the X value under Rotation to 180. The player flips upside down.
Now, when you click on Play to test the game, the player will break-dance his way around the AngryBots Demo, electric boogaloo-style. The robots are freaking out, thinking "ERROR! DOES NOT COMPUTE!" Way to keep them on their mechanical toes.
Above the Inspector panel, you'll see the Layers and Layout dropdowns. Game Objects can be grouped into layers, somewhat like in Photoshop or Flash. Unity stores a few commonly used layouts in the Layout dropdown (mine is set to the "2 by 3" configuration). You can also save and load your own custom layouts.
These three buttons help you test your game and control playback. As you've seen, the Play button starts and stops your game. The Pause button works as expected it pauses your game so that you can make changes to it on the fly. The third button is a Step-Through control; use it to advance frame-by-frame through your game so that you can more tightly control what's going on.
Changes you make while testing don't stick!
One of the more surprising features of Unity is that you can make changes to Game Objects and variables on the fly while you're testing your game. But it's important to know that the changes you make during testing will not "stick". Once you stop testing your game, the changes that you made during testing will revert to the state they were in before you clicked on the Play button. It's disheartening to make a number of changes to your game, only to realize that the Play button was on the entire time, and your changes will be lost. One way to avoid this problem is to toggle the Maximize on Play button in the Game window so that you'll be more aware of when you're testing and when you're not.
At the top-left of your screen, you'll see four controls that help you move around your Scene, and position Game Objects within it. These controls are mapped to the Q, W, E, and R keys on your keyboard. From left to right, they are:
The Hand tool (Q): Use it to click-and-drag around your scene. Hold down the Alt key on your keyboard to rotate the view. Hold down the Ctrl key (Windows) or the Command/control key (Apple) to zoom in and out. Your mouse wheel will also zoom the scene. Hold down the Shift key to pan, zoom, and rotate in larger increments to speed things up. This is a way for you to navigate around the game world. It doesn't actually impact the way the player sees the game. To modify the Game view, you need to use the Move or Rotate tools to modify the Camera position.
The Move tool (W): This tool lets you move the Game Objects around your scene. You can either drag the object(s) around by the X, or Y, or Z-axis handles, or by the square in the center for freeform movement. Holding down the Ctrl key or Command key (Apple) will snap movement to set grid increments. Hold down Shift at the same time to snap objects to the "floor".
Scale tool (R): This tool works much the same as the Move and Rotate tools. Use it to make your Game Objects larger or smaller. Dragging an X, Y, or Z handle will non-uniformly scale (squash and stretch) the object, while dragging the gray cube in the center will uniformly scale it.
We've glanced briefly at the key elements of the Unity interface, but there's no need to stop poking around. Far beyond the scope of this book, there is a wealth of menu options, buttons, and controls that we haven't covered. Why not explore those menus or start randomly clicking on things that you don't yet understand? Now is the time to safely break stuff. You didn't work hard to create the AngryBots Demo, so why not mess around with it a little bit?
Here are some things to try:
Select some of the Game Objects in the Hierarchy panel and move them around in the Scene window using the Scene controls. What happens when you put an airlock in the middle of the sky? Can the player still pass through? What if you put the cannisters or the computers over the player's head before the game starts? Do they fall, or do they hover? Can you remove objects to help the player careen off the edge of the balcony? What happens when he does?
Randomly right-click in the three different panels and read through the context menu options to see what you're getting yourself into.
Poke around in the Game Object | Create Other menu. There's a whole list of interesting things that you can add to this scene without even touching the 3D modeling program.
What happens when you delete the lights from the scene? Or the camera? Can you add another camera? More lights? How does that affect the Scene?
Can you move the player to another part of the demo to change your starting position?
Can you replace the audio files to make the gun "moo" whenever you fire it?
Download a picture of kittens from the Internet and see if you can wrap it around a boulder model. Kittens rock! You can pull the kitties into your project using the Assets | Import New Asset option in the menu.
A tuner's paradise
The Unity 3D interface is designed to be customized. Not only can you define your own custom window layouts, but you can even write custom scripts to make certain buttons and panels appear inside Unity to speed up your workflow. That kind of thing is well beyond the scope of this book, but if you're the kind of person who really likes to get under the hood, you'll be happy to know that you can tweak Unity 3D to your heart's content maybe add a few racing stripes and install an enormous pair of subwoofers in the back?
Chapter 1 was all about getting a feel for what Unity can do and for what the program interface had to offer. Here's what we found out:
Massive 80 person teams, all the way down to tiny one or two person teams are using Unity to create fun games.
By thinking small, we'll have more success in learning Unity and producing fully functional games instead of huge but half-baked abandoned projects.
Different flavors of Unity help us deploy our games to different platforms. By using the free indie version, we can deploy to the Web, the Mac, and PC platforms.
The Unity interface has controls and panels that let us visually compose our game assets, and test games on the fly right inside the program!
I hope you've taken some time to thoroughly vandalize the Bootcamp Demo. If you save the file by clicking on File | Save Project, you'll have a perma-upside-down space marine in your demo. If you want to return to a pristine AngryBots Demo later to wreak more havoc, don't bother saving the hilarious (but meaningless) changes we've made in this chapter.
Now that we've trashed this joint, let's take a quick trip through some game design theory. In the next chapter, we'll figure out the scope and scale of a game that a solo, beginner developer should actually tackle. Crack your knuckles and put on your favorite hat because you're about to dip yourself in awesome sauce.