O'Reilly logo

Unity 3D Game Development by Example by Ryan Henson Creighton

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - add the lose condition

The easiest way to figure out if the player has lost is to check the transform.position.y value of the heart. If the heart has fallen through the floor, the player obviously isn't bouncing it on the tray any longer.

  1. Add the logic for incrementing the player's score (number of hits/bounces before losing):
    function OnCollisionEnter(col : Collision) {
    if(col.gameObject.tag == "tray") {
    //Debug.Log("yes! hit tray!");
    if (!velocityWasStored) {
    storedVelocity = rigidbody.velocity;
    velocityWasStored = true;
    }
    if(rigidbody.velocity.y > 1) {
    numHits ++;
    }
    rigidbody.velocity.y = storedVelocity.y;
    }
    
  2. Add the "lose the game" check to the Update function:
    function Update() { var str:String = ""; if(!hasLost){ str = numHits.ToString(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required