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 how can I make the player take damage, wait 2 seconds and take another damage?

Here is the damage code:

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;

    void Start()
    {
       
    }

    void Update()
    {

    }
    void OnTriggerEnter2D(Collider2D outro)
    {  
        if(outro.gameObject.CompareTag("Dano"))
        {  
            if(valorAtual > 0)
            {
                valorAtual -= dano;
                lifeBar.fillAmount = (float)valorAtual / 10;
            }
            else if(valorAtual <= 0)
            {  
                Vivo = false;
                Destroy(gameObject);
            } 
        }
    }
}

You have two options.

Use OnTriggerEnter with OnTriggerExit and one of Update methods. You turn on damage taking in onEnter and turn off in onExit,

Second option is to use OnTriggerStay method.

1 Like

Hey man, I did this:

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)
            {  
                StartCoroutine("TempoDeAtaque");
                valorAtual -= dano;
                lifeBar.fillAmount = (float)valorAtual / 10;
            }
            else if(valorAtual <= 0)
            {  
                Vivo = false;
                Destroy(gameObject);
            } 
        }
    }

    IEnumerable TempoDeAtaque()
    {
        podeAtacar = false;
        yield return new WaitForSeconds(5.0f);
        podeAtacar = true;
    }
}

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

What do I do? ;-;

First of all remove the coroutine and just forget about them.

Second I will not write for you the code, but let me give you some hints.
OnTriggerStay will be called every frame, so you have to accumulate the time object stayed inside a trigger and do damage when particular time elapsed. Sine on triggerStay is called every frame you will have to use Time.fixedDeltaTime.

1 Like

So bro, sorry to bother, but I don’t know where to use this Time.fixedDeltaTime, I’m still learning to mess with C#

I was researching about it, and how to use it but I didn’t understand anything ;-;

Could you tell me where I put this Time.fixedDeltaTime please? ;-;

I think you should do some basic tutorials on c# and unity3d. It’s not like you just put fixedDeltaTime in some particular place and it will work. You have to create some code around it.

1 Like