Time for action – adding 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.
Add the logic for incrementing the player's score (number of hits/bounces before losing):
function OnCollisionEnter(col : Collision) { if(col.gameObject.CompareTag("tray")) { //Debug.Log("yes! hit tray!"); if (!velocityWasStored) { storedVelocity = rigidbody.velocity; velocityWasStored = true; } if(rigidbody.velocity.y > 1) { numHits ++; } rigidbody.velocity.y = storedVelocity.y; }
Add the "lose the game" check to the
Update
function:function Update() { var str:String = ""; if(!hasLost){ str = numHits.ToString(); } else { str = "Hits:" + numHits.ToString() + "\nYour best:" + bestScore; if(bestScore > lastBest) str += "\nNEW RECORD!"; } hitCount.text...