Handling Player Death

Now that we can deal damage, it’s entirely possible that the player will run out of health and perish. We’ll hook this into a proper Game Over screen in Chapter 13, “Bringing It All Together,” but for now, let’s do something quick and easy to represent the loss.

Let’s make the changes to our PlayerStats script shown in Listing 9.9.

Listing 9.9 Handling Death in the PlayerStats Script

private bool isDead = false;public void TakeDamage(int damage, bool playHitReaction){  if(this.isImmune == false && isDead == false)  {    this.health = this.health - damage;    Debug.Log("Player Health: " + this.health.ToString());    if(this.health <= 0)    {      PlayerIsDead();    }    else if(playHitReaction ...

Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation 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.