Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How might I calculate Kills Per Second with the addition of a cool down?

Discussion in 'Scripting' started by Zapleaf, Apr 2, 2016.

  1. Zapleaf

    Zapleaf

    Joined:
    Feb 22, 2013
    Posts:
    9
    I know this has got to have an easy answer, and it may very well be the fact that I've put too much thought into it for too long that my brain just hit a no-pass zone.. but the question is simple.

    Very simple visual example.
    Damage = 7;
    Attack Delay = 1.0;
    Enemy HP = 10;
    Enemy spawn delay = 1.2;

    You can skip this example if you already know a solution:
    Now with the given example, and a few assumptions, there is zero delay between the first damage hit and when the enemy spawns. How can I calculate how many kills the player will get in 1 second of time? The answer does not even need to be a whole number, it could be 1.5 kills per second, or 0.7 kills per second. I've tried a lot of ways to get this right and right now this is the closest I've gotten to an answer: ((Damage * Attack Delay) / HP) / (1 + Spawn Delay). This works somewhat.. I get the correct answer when the spawn delay is below 1.0 but when it is above 1.0 the solution reports a value twice the correct number. It would work Perfectly without the '1' in (1 + Spawn Delay) but when the value is below 1.0 the reported number begins to change in the wrong direction.
     
  2. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    Code (CSharp):
    1.  
    2. public float damage = 7f;
    3. public float attackDelay = 1f;
    4. public float hp = 10f;
    5. public float spawnDelay = 1.2f;
    6.  
    7. float attacksPerSecond = 1 / attackDelay;
    8.   // attacksPerSecond  = 1 / 1 = 1
    9.   // (convert attack delay to attacks per second)
    10.  
    11. float spawnRate = 1 / spawnDelay;
    12.   // spawnRate  = 1 / 1.2 = 0.833
    13.   // (convert spawn delay to spawns per second)
    14.   // (creates ~83% of an enemy in a second, or spawns 0.833 enemies per second)
    15.  
    16. float killStrength = damage/hp;
    17.   // killStrength = 7/10 = 0.7
    18.   // (or 70% of targets health per attack)
    19.  
    20. // kills per second = kill strength * attacks per second;
    21. // kills per second = 0.7 * 1 = 0.7
    22. // (deals 70% of targets health per second, or kils 0.7 enemies per second)
    23.  
    24. //Full Formula (reduced):
    25. //  kills per second  = (Damage / HP) * (1 / Attack Delay)
    26. //                    = Damage * Attack Delay / HP
    27. float killsPerSecond = damage * attackDelay / hp;
    28.  
     
    kru and Zapleaf like this.
  3. Zapleaf

    Zapleaf

    Joined:
    Feb 22, 2013
    Posts:
    9
    That was incredibly thorough, thank you very much! I was banging my head on this problem for a while. I did try a time or two to break it down into chunks like you did, but never quiet got to the correct answer like you have.
     
  4. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    The kills per second will actually be the minimum of kills per second and spawn rate. Consider an attacker who attacks 10 times per second and does 100% of an enemy's health per attack. That attacker's kills per second is calculated to be 10 enemies per second. However, it will be effectively limited by the spawn rate of 83% of an enemy per second.