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