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(); ...

Get Unity 3D Game Development by Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.