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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Individual Calculations for OnTriggerEnter2D

Discussion in 'Scripting' started by Smaex26, Nov 17, 2022.

  1. Smaex26

    Smaex26

    Joined:
    Dec 15, 2020
    Posts:
    5
    Hello there!

    I ran into a problem regarding the OnTriggerEnter2D Method. Here is what I have so far:

    I have a Hitbox (attack) object with a script that is running OnTriggerEnter2D. I'm checking for everything that has the tag "Enemy" on there. Then I check if the the hitbox is going to do normal or critical damage. And here is the problem. I want to calculate the cricitcal chance for every detected collision individually. So I created a List and added every collider to the list and ran a for loop for every collider checking for critical damage. But now the more enemies I hit the higher the damage output is going to be...

    Here is the script:

    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D col)
    2. {
    3.         if (col.CompareTag("Enemy"))
    4.         {
    5.             if (!enemies.Contains(col))
    6.             {
    7.                 enemies.Add(col);
    8.             }
    9.  
    10.             for (int i = 0; i < enemies.Count; i++)
    11.             {
    12.                 if (enemies[i] != null)
    13.                 {
    14.                     if (!enemies[i].GetComponent<EnemyController>().invincible)
    15.                     {
    16.                         float tmp = Random.Range(0, 100);
    17.                        
    18.                         if (tmp < (critChance + pStats.critChanceMod))
    19.                         {
    20.                             //Crit
    21.                             damage = (damage * (1 + (pStats.damageMod / 100))) * (1+ (((critDamage - 100) * (1+(pStats.critDamageMod/100)))/100));
    22.                         }
    23.                         else
    24.                         {
    25.                             //Normal
    26.                             damage = damage * (1 + (pStats.damageMod / 100));
    27.                         }
    28.        
    29.                         enemies[i].GetComponent<EnemyController>().GetDamage(damage);
    30.                     }
    31.                    
    32.                     Debug.Log(damage);
    33.                 }
    34.             }
    35.             Debug.Break();
    36.         }
    37. }
    The enemies destroy themselves when their health is 0.. maybe the for loop gets confused when something is getting destroyed in the same frame? I dont know :D

    I don't get why the damage is staggering up the more enemies it hits... Is there a simpler method to do this?
    Thanks in advance!

    Max
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Not only are you trying to maintain a list of things you hit, you're not removing them during OnTriggerExit2D.

    I think you're making this hard for yourself. You can replace this by having a List<Collider2D> that you reuse then perform a query with the "trigger" collider above using Collider2D.OverlapCollider, filtering by the "Enemy" layer. This way, you'll be returned a list of any colliders on the Enemy layer (you don't need to use tags). You only then have to iterate the list.

    Of course, this would depend on whether this was continuous damage or a one-off damage. If it's a one-off damage until you separated then touched again then callbacks might be easier. But in the case above for one-off damage, I don't understand why they need to be in a list. You'll only get this callback once when it first hits and won't again until it separates then contacts again.
     
    Smaex26 likes this.
  3. Smaex26

    Smaex26

    Joined:
    Dec 15, 2020
    Posts:
    5
    Thank you! That did the trick! There was also something wrong in my crit calculations which caused the staggered damage amount but I fixed it. Everythings working now, thank you!
     
    MelvMay likes this.