Book Image

Unity 4.x Game Development by Example: Beginner's Guide - Third Edition

By : Ryan Henson Creighton
Book Image

Unity 4.x Game Development by Example: Beginner's Guide - Third Edition

By: Ryan Henson Creighton

Overview of this book

Unity is one of the biggest game engines in the world, providing the user with a range of important tools that they need to bring their ideas into reality. Beginner game developers are optimistic, passionate, and ambitious, but that ambition can be dangerous! Too often, budding indie developers and hobbyists bite off more than they can chew. Games like Angry Birds, Cut the Rope, and Fruit Ninja are fun, simple games that have delighted players and delivered big profits to their creators. This is the perfect climate for new game developers to succeed by creating simple games with Unity, starting today. This book teaches you the ins and outs of the unique Unity game engine interface. Clear and concise code examples written in both Unity Javascript and C# take you through the step-by-step process of building five small, functional games. With this understanding you can start making your own mark on the game industry! With absolutely no programming or game development experience, you will learn how to build five simple games in Unity by following step-by-step instructions, peppered with amusing analogies and anecdotes from an experienced indie developer. Following a primer on simplifying your game ideas to that single “something” that keeps players coming back for more, dive into the Unity game engine by creating a simple bat-and-ball game. From there, you'll build a complete memory game using only the Unity GUI system. After building a 2.5D mouse avoider game, you'll learn how to re-skin the project to completely change the game's theme. Incorporating everything you've learned, you'll return to complete the bat-and-ball game by adding scoring, replay flow, sound effects, and animations. Finally, in the new bonus chapter, you'll program some simple AI (Artificial Intelligence) for a tic tac toe game. "Unity 4.x Game Development by Example" is a fun and light-hearted exploration of one of the most powerful game engines on the market today. Find out what all the fuss is about by getting up to speed using this book!
Table of Contents (22 chapters)
Unity 4.x Game Development by Example Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – adding the Play Again button


The very last thing we need to do is to add a button to the end of the game so that the player can play again. Let's revisit our good friend GUI from the last few chapters.

  1. Add the OnGUI function to the HeartBounce script:

    function OnGUI(){
      if(hasLost){
       var buttonW:int = 100; // button width
       var buttonH:int = 50; // button height
       
       var halfScreenW:float = Screen.width/2; // half of the Screen width
       var halfButtonW:float = buttonW/2; // Half of the button width
    
       if(GUI.Button(Rect(halfScreenW-halfButtonW, Screen.height*.8, buttonW, buttonH), "Play Again"))
       {
          numHits = 0;
          hasLost = false;
          velocityWasStored = false;
          transform.position = Vector3(0.5,2,-0.05);
          rigidbody.velocity = Vector3(0,0,0);
       }
      }
    }
    

What just happened?

For GUI pros like us, this script is a piece of cake.

The whole function is wrapped in a "has the player lost the game?" conditional statement.

We start by storing some values...