Search code examples
c#unity-game-enginevirtual-reality

Damage Over Time game mechanic on multiple objects (90FPS/VR Problems)


I'm not looking for a spoon fed solution but perhaps some insight because I have tried many different scenarios in attempt to get good performance.

I have a scenario where I would like to apply damage over time to X amount of enemies (Zombies), from a collider (Projectile).

Current Problematic Setup

Currently, I have a script attached to my Projectile which will StartCoroutine onTriggerEnter that will decrement the Zombies life over time. This works wonderfully, until you want to damage more than 15 or so Zombies at time.

I presume this is from some of the magic going on in all of the StartCoroutines. Here is my couroutine thats called on OnTriggerEnter.

IEnumerator DoDOT(float damageDuration, int damageCount, float damageAmount)
    {
        damagingOverTime = true;
        int currentCount = 0;
        while (currentCount < damageCount)
        {
            zombieHealth -= damageAmount;
            yield return new WaitForSeconds(damageDuration);
            currentCount++;
        }
        damagingOverTime = false;
    }

Possible Solution

Now I've done a simple test (with double the amount of zombies) where I've hard coded the Damage Over Time mechanic onto the Zombies Update function. Of course when I start the scene, every single Zombie begins to take damage over time, but there are no performance issues.

I'm thinking I could have the update in my Zombie game object "watching" for a Damage Over Time flag, then my Projectile can activate that flag OnTriggerEnter. The Zombie Update will than begin to apply Damage over time. Could those with more experience let me know if this is the correct approach, and if not, what is?

Performance goal 90 FPS not 60FPS. My current process of starting co-routines for the DOT would actually work fine in theory in a 60FPS environment because you have a lot more room for processing before it could cause a frame drop. But when you aim for 90FPS that same coroutine can not handle it because we no longer have that extra processing room. I'm trying to figure out why my Possible Solution provides more efficient results in a 90FPS environment, or what are possible solutions for more efficient results in a 90FPS environment.

PS - It's not really another zombie shooter, those terms are just examples :D


Solution

  • Some debugging is needed here. You can do something like count the numbers of times this function is called just to make sure it's not called too many times.

    Another thing you can try to do is move this to the Update method of the projectile itself and keep a list of all enemies that were hit by this projectile (simply add them to the list in OnTriggerEnter). So instead of having many Coroutines running in the background everything is handled on a simple loop that loops through all the enemies that were hit by it.

    P.s writing this on mobile sorry for any mistakes