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;
}
}