public void ChangeHealth(float changeValue) { health += changeValue; if (health > healthMax) { health = healthMax; } if (healthBar) { healthBarFill.fillAmount = health / healthMax; } if (changeValue < 0 && gameController.playerObject == this) { Camera.main.transform.parent.GetComponent <Animation>().Play(); } if (health <= 0) { if (gameController.playerObject && gameController.playerObject != this) { DelayedDie(); } else { Die(); } if (gameController.playerObject && gameController.playerObject == this) { gameController.SendMessage("GameOver", 1.2f); Time.timeScale = 0.5f; } } if (gameController.playerObject && gameController.playerObject == this && gameController.healthCanvas) { if (gameController.healthCanvas.Find("Full")) { gameController.healthCanvas.Find("Full").GetComponent <Image>().fillAmount = health / healthMax; } if (gameController.healthCanvas.Find("Text")) { gameController.healthCanvas.Find("Text").GetComponent <Text>().text = health.ToString(); } if (gameController.healthCanvas.GetComponent <Animation>()) { gameController.healthCanvas.GetComponent <Animation>().Play(); } } }
/// <summary> /// Changes the lives of the player. If lives reach 0, it's game over /// </summary> /// <param name="changeValue"></param> public void ChangeHealth(float changeValue) { // Change the health value health += changeValue; // Limit the value of the health to the maximum allowed value if (health > healthMax) { health = healthMax; } // Update the value in the health bar, if it exists if (healthBar) { healthBarFill.fillAmount = health / healthMax; } // If this is the player car, play the shake animation if (changeValue < 0 && gameController.playerObject == this) { Camera.main.transform.parent.GetComponent <Animation>().Play(); } // If health reaches 0, the car dies if (health <= 0) { if (gameController.playerObject && gameController.playerObject != this) { DelayedDie(); } else { Die(); } // If this is the player car, trigger the GameOver event if (gameController.playerObject && gameController.playerObject == this) { gameController.SendMessage("GameOver", 1.2f); // Play a slowmotion effect Time.timeScale = 0.5f; } } // Update the health bar if (gameController.playerObject && gameController.playerObject == this && gameController.healthCanvas) { // Update the health bar based on the health we have if (gameController.healthCanvas.Find("Full")) { gameController.healthCanvas.Find("Full").GetComponent <Image>().fillAmount = health / healthMax; } if (gameController.healthCanvas.Find("Text")) { gameController.healthCanvas.Find("Text").GetComponent <Text>().text = health.ToString(); } // Play the animation of the health icon if (gameController.healthCanvas.GetComponent <Animation>()) { gameController.healthCanvas.GetComponent <Animation>().Play(); } } }