How to become invincible towards enemies when powerup is active
I am trying to find a way so that when my player comes in contact through a trigger on the enemy it will make the play invincible and not lose any health, can you give me hints on how to do that.
Answer by GDGames0302 · Feb 03, 2021 at 01:25 PM
Hi. It depends on the behaviour you want. When you collect a powerup, you can make a bool true and start a coroutine with a WaitForSeconds to set it back to false after a certain time and in the function that checks for collision between the player and the enemies you execute that function only if that bool is false.(or you can execute the function even if the bool is not false, but decrease the health only if that bool is false). It really depends on what you want to achieve.
Answer by Zaitoichi · Feb 03, 2021 at 03:17 PM
This is what i use in my game.
public void DestroyShip()
{
if (!invincible)
{
animator.SetBool("Explode", true);
destroyOverLifetime.shouldWait = false;
FindObjectOfType<PlayerMovement>().CantMove();
invincible = true;
if (PlayerDataManager.instance.lives > 0 ) {
PlayerDataManager.instance.RemoveExtraLife();
}
else
{
SceneManager.LoadScene("GameOver");
}
}
}
if the ship is hit. the DestroyShip funtion is called. Then if the bool invincible is true. nothing happens.
p.s. the naming of my method is not the way to go.. Renaming it right now :)