so i setup a way to take damage when under water but if i leave and enter and leave and enter ect. the damage stacks and instead of taking one tick of damage itll go to 2 then 3 the more i exit and enter the tag
public class LungCapacity : MonoBehaviour
{
[SerializeField] float lungCapacity;
[SerializeField] float timeBetweenTicks = 3f;
[SerializeField] float underwaterHealthChange;
[SerializeField] int numberOfTicks = 100;
[SerializeField] GameObject player;
float lungHealthPerTick;
float ticksRemaining;
bool isUnderWater = false;
Health health;
private void Awake()
{
health = GetComponent<Health>();
}
private void Update()
{
if (isUnderWater == true)
{
Debug.Log("You Are UnderWater");
Debug.Log(lungHealthPerTick);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("UnderWater") == true)
{
isUnderWater = true;
if(isUnderWater == true)
{
StartCoroutine(HealthChangeOverTime(health));
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("UnderWater") == true)
isUnderWater = false;
if (isUnderWater == false)
{
StopCoroutine(HealthChangeOverTime(health));
}
}
public IEnumerator HealthChangeOverTime(Health health)
{
lungHealthPerTick = underwaterHealthChange / numberOfTicks;
ticksRemaining = numberOfTicks;
// while (ticksRemaining > 0 && !health.IsDead())
while (isUnderWater == true)
{
if (isUnderWater)
{
health.TakeDamage(player, lungHealthPerTick);
}
else
{
health.Heal(lungHealthPerTick);
}
yield return new WaitForSeconds(timeBetweenTicks);
ticksRemaining--;
}
yield break;
}
}
any ideas how i can handle this?