One_Eyed_Teddy
One_Eyed_Teddy

Reputation: 13

Damage over time effect is destroying my frame rate, unity c#

OK hi everyone. I'm pretty new to scripting and i am learning by making a game. I am trying to apply a damage over time effect to my player. Though i have something that works, when the timers are up my frame rate drops from over 60 to below 10. So i am hoping someone can help explain why this is, if my method is viable and is there anything better i could do. here is my code.

public class AllDamage : MonoBehaviour 
{
//Type of Damage to be applied when attatched to a game object
public bool isFire;
public bool isIce;
public bool isElectric;
public bool isWind;
public bool isBlunt;
public bool isSharp;
public bool isToxic;
public bool isPoisen;

public bool isTicking; // is this damage over time

public int ticks = 0;
public int DotDuration;

// Ammount or initial damage
public float ammountHealthDmg = 10f;
public float ammountPhysicalDmg = 10f;
public float ammountMentalDmg = 10f;
// ammount of damage over time
public float tickHealthDmg = 1f;
public float tickPhysicalDmg = 1f;
public float tickMentalDmg = 1f;



void Update()
{
    if(isTicking == true)
    {
        applyDoT ();
        StartCoroutine("CountSeconds");
    }
}

IEnumerator CountSeconds()
{
    int DoTDuration = 0;
    while(true)
    {
        for (float timer = 0; timer < 1; timer += Time.deltaTime)
        yield return 0;
        DoTDuration++;
        Debug.Log(DoTDuration + " seconds have passed since the Coroutine started.");

        if(DoTDuration == 10)
        {
            StopCoroutine("CountSeconds");
            isTicking = false;
        }
    }
}
void applyDoT()
{
    // ticks increments 60 times per second, as an example
    ticks++;
    // Condition is true once every second
    if(ticks % 60 == 0)
    {
        decrementStats(tickHealthDmg, tickPhysicalDmg, tickMentalDmg);
    }
}

// is this the player?... what am i?...apply my inital damage...do i have damage over time
void OnTriggerEnter(Collider other)
{
    if(other.tag == "Player")
    {
        if(isFire)
        {
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg);
            isTicking = true;
        }
        if(isIce)
        {
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg);
            isTicking = true;
        }
        if(isElectric)
        {
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg);
        }
        if(isWind)
        {
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg);
        }
        if(isBlunt)
        {
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg);
        }
        if(isSharp)
        {
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg);
            isTicking = true;
        }
        if(isToxic)
        {
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg);
            isTicking = true;
        }
        if(isPoisen)
        {
            decrementStats(ammountHealthDmg, ammountPhysicalDmg, ammountMentalDmg);
            isTicking = true;
        }
    }
}
// - stat values
void decrementStats(float health, float physical, float mental)
{
    Stats.health -= health;
    Stats.physical -= physical;
    Stats.mental -= mental;
    Stats.CheckAllStats();
}

}

Upvotes: 1

Views: 876

Answers (1)

Disenchanted
Disenchanted

Reputation: 613

You are starting your coroutine multiple times, once per frame, by calling StartCoroutine() in Update() callback. What you should do, is set a shouldStartTicking (it's called isTicking in your code) flag to true when you want to start the coroutine and then set it to false immediately in the coroutine. But a much cleaner solution would be to start the coroutine right from the code that registered a hit. I would also suggest you to call the decrementStats method from the coroutine, instead of using an external counter-variable to convey the state of your DoT effect.

Upvotes: 4

Related Questions