Hi, I am trying to use a co-routine to make my player invulnerable to damage for a few seconds after being hit. I tried the following code, but the player can still constantly take damage. Any suggestions would be great! Thanks!
I have my private bool invul = false; then…
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Enemy") { // If colliding with enemy, execute this block
HealthScript playerHealth = this.GetComponent<HealthScript> (); //get player health script
playerHealth.Damage (1); //damage player
invul = true; //player is invulnerable
animator.SetTrigger ("Hit"); //play hit animation
StartCoroutine (InvulWait ());
}
}
private IEnumerator InvulWait() {
yield return new WaitForSeconds (2); //invul time
invul = false;
}
}