- Home /
Need help making player invincible momentarily
So I'm designing a simple rpg where the enemies hurt you by getting close to you. So it gives the sense that they are hurting you by touching you. I have an update function on the enemy that checks how close the player is to the enemy. Once the enemy is within it's damaging radius, it calls the apply hit function on the player and hurts them. The problem however is that the update function calls the apply hit function 70 something times a second and immediately kills the player. Does anyone know a good way to make the player invincible for a second or two? Thanks
Answer by MezFo · Oct 28, 2017 at 03:25 AM
Making your player invincible is not the solution. You should use IEnumerator if you want your enemy to attack only once per certain amount of time along with a bool.
like this -
void Update() {
//add bool to if statement that checks distance
if(distance < 5 && attacking == false) {
attacking = true;
//Start the Ienumerator function
StartCoroutine (Hit ());
}
IEnumerator Hit() {
//put how many seconds you want it to wait in WaitForSeconds(here)
yield return new WaitForSeconds(1);
//Whatever you have below the yield (above) will run after the amount of seconds you want have passed
print("IEnumerator is awesome!");
attacking = false;
}
You don't want something in your update unless you want it to run every frame even if you could disguise it with something like making the character invincible it would be terrible for performance.
Your answer
Follow this Question
Related Questions
Zombie Alien Mellee Attack 1 Answer
Bayonet Action Transform 0 Answers
script on monster checks if bool on attacking player = true? 2 Answers
Melee attacking, using force and health values 1 Answer
AI Help FPS 0 Answers