In my player movement script I am trying to create an item drop that makes the player invincible for 3 seconds. After 3 seconds i want the player to be able to die again when it collides with an enemy. I have the collision part of it working just fine, but I can’t figure out how to create a 3 second delay within my OnCollisionEnter2D method. Basically, after the player collides with the drop it will set bool canDie to false, making the player invincible to enemies. Then there needs to be the 3 sec delay, followed by setting bool canDie back to true, disabling invincibility. I have looked at Invoking and Coroutines/Wait for seconds but I am not experienced with those aspects and cannot get the delays to work. Any help is appreciated. Thanks.
private bool canDie = true;
void OnCollisionEnter2D(Collision2D col)
{
//Collision with enemy
if (col.gameObject.tag == "Enemy" & canDie == true)
{
Destroy(gameObject);
//Game over screen
if (Application.loadedLevel == 1)
{
LoadScene(2);
}
}
//Collision with Invincibility Drop
if (col.gameObject.tag == "Drop1")
{
Debug.Log ("COLLISION WITH DROP");
canDie = false; //If player picks up drop, player becomes invincible. After 3 seconds, invincibility is gone.
//PLACE 3 SECOND DELAY HERE
canDie = true;
}
}