public void TakeDamage(float damage) { UI_FloatingHealthBar hb = GetComponent <UI_FloatingHealthBar>(); AudioSource aud = GetComponent <AudioSource>(); if (!dead) { if (currentHitPoints <= 0) { Transform uiCanvas = FindObjectOfType <Canvas>().transform; var gameOverMsg = Instantiate(gameOverPopup) as GameObject; gameOverMsg.transform.SetParent(uiCanvas); gameOverMsg.transform.localScale = new Vector3(1, 1, 1); if (wg) { wg.EndWaves(); } else if (wgt) { wgt.EndWaves(); } Destroy(hb.healthBar.gameObject); gameObject.GetComponent <SpriteRenderer>().sprite = destroyed; dead = true; Instantiate(destroyed, transform.position, transform.rotation); aud.Stop(); aud.PlayOneShot(destructionSound, .5f); } else if (currentHitPoints <= maxHitPoints / 3) { hb.healthBar.color = Color.red; gameObject.GetComponent <SpriteRenderer>().sprite = twoThirdDamage; } else if (currentHitPoints <= 2 * (maxHitPoints / 3)) { hb.healthBar.color = Color.yellow; gameObject.GetComponent <SpriteRenderer>().sprite = thirdDamage; } } if (currentHitPoints > 0) { currentHitPoints -= damage; if (hb.healthBar) { hb.healthBar.rectTransform.localScale = new Vector3(Mathf.Max(0, (currentHitPoints / maxHitPoints)), 1, 1); } //Make this play through before playing the next clip //aud.Stop(); if (!aud.isPlaying) { aud.PlayOneShot(damageSound, Random.Range(.8f, 1.0f)); } } }
// Use this for initialization protected virtual void Start() { this.hb = GetComponent <UI_FloatingHealthBar>(); this.zombieState = ZombieState.Normal; // set travel direction if (currentNode) { this.direction = currentNode.transform.position - this.transform.position; } // save reference to particle systems for later use this.psLarge = this.transform.Find("ParticleSystemFireLarge").GetComponent <ParticleSystem>(); this.psSmallLeft = this.transform.Find("ParticleSystemFireSmallLeft").GetComponent <ParticleSystem>(); this.psSmallRight = this.transform.Find("ParticleSystemFireSmallRight").GetComponent <ParticleSystem>(); hitPoints = maxHitPoints; }