How do I make the player take continuous damage?

Here’s the thing, in my game there’s the enemy, and I need him to damage the player if the player enters his Collider’s area, I’ve already managed to do that, but the problem is that the player takes damage once and then stops taking damage, the player only takes another damage if he leaves the Collider’s area and enters again.

So I modified the code so that it takes damage wait 5 seconds and then take another damage

But when the player enters the enemy’s Collider he takes several damages in a row, and dies almost instantly.

Someone told me to use Time.fixedDeltaTime, but I don’t know where to put it

Here is the code, I will be very grateful if someone help me:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class VidaPlayer : MonoBehaviour
{
     public bool Vivo = true;
     public Image lifeBar;
     public int valorAtual = 10;
     public int dano = 1;
     public bool podeAtacar = true;
     void Start()
     {
        
     }
     void Update()
     {
     }
     void OnTriggerStay2D(Collider2D outro)
     {  
         if(outro.gameObject.CompareTag("Dano"))
         {  
             if(podeAtacar == true && valorAtual > 0)
             {  
                 valorAtual -= dano;
                 lifeBar.fillAmount = (float)valorAtual / 10;
                 StartCoroutine("TempoDeAtaque");
             }
             else if(valorAtual <= 0)
             {  
                 Vivo = false;
                 Destroy(gameObject);
             } 
         }
     }
     IEnumerable TempoDeAtaque()
     {
         podeAtacar = false;
         yield return new WaitForSeconds(5.0f);
         podeAtacar = true;
     }
}

I suspect the most likely culprit is that you’re using OnTriggerStay when you should be using OnTriggerEnter, the reason you are taking several bits of damage in a row is because OnTriggerStay triggers your code every frame whereas Enter does it once.

1 Like

So what I want is for the character to take damage, wait 5 seconds and take another damage, understand?

Why did you make another thread instead of using your old one? You’re spamming the forum.