Book Image

Learning Unity 2D Game Development by Example

By : Venita Pereira
Book Image

Learning Unity 2D Game Development by Example

By: Venita Pereira

Overview of this book

<p>If you are looking for a guide to create 2D games using Unity, look no further. With this book, you will learn all the essentials of 2D game development by creating five real-world games in a step-by-step manner throughout the course of this book.</p> <p>Starting with a blank scene, you will learn all about the new Unity 2D toolset, which will enable you to bring your scene to life. You will create characters, make them move, create some enemies, and then write code to destroy them. After figuring out all the necessities of creating a game, this book will then assist you in making several different games: games with collision, parallax scrolling, Box2D, and more.</p> <p>By the end of this book, you will not only have created several small games, but you will also have the opportunity to put all your new-found knowledge into creating and deploying a larger, full game.</p>
Table of Contents (17 chapters)
Learning Unity 2D Game Development by Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Game conditions


Game conditions are the rules that dictate a victory or loss. This game will include the following conditions:

  • Lives

  • Timer

  • Score

Lives

The player will start the game with three lives. Each time Spongy comes into contact with the acid blobs, the player will lose a life.

Should the player lose all three lives, then the game is over. To stop gameplay, we use the following line:

Time.timeScale = 0;

It is also useful for not only pausing and resuming, but also for slow and fast forward effects.

Add the following script to Spongy.js:

var lives:int = 3;


function OnCollisionEnter2D(other:Collision2D)
{
    if(other.gameObject.name=="Acid(Clone)")
    {
      lives = lives - 1;

    if (lives == 0)
    {
    Time.timeScale = 0;

    }

  }

  //will add score here

}

Score

The main objective of victory is the score. For each grime that Spongy collects, the player receives an added score of 50 points.

  1. Add the following variable to Spongy.js at the very top:

    var score:int = 0;
  2. Insert the following...