Search Unity

Hitting Multiple Enemies in One Strike?

Discussion in 'Scripting' started by PowerRidden, Mar 27, 2015.

  1. PowerRidden

    PowerRidden

    Joined:
    Jun 24, 2014
    Posts:
    8
    I was wondering what the theory behind hitting multiple enemies with one strike might be. For example: If I shot out a lightning bolt and it hit one enemy and there were two more close to him it would hit the other two as well as the original. I was thinking something along the lines of getting the closest transforms after hitting the first enemy but I'm really not sure about this one and any help would be appreciated.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Something like a sphere cast or an overlap sphere may help.
     
  3. PowerRidden

    PowerRidden

    Joined:
    Jun 24, 2014
    Posts:
    8
    Thanks man, I'll definitely look into that.
     
  4. PowerRidden

    PowerRidden

    Joined:
    Jun 24, 2014
    Posts:
    8
    Okay so I went onto the unity documentation for scripting and I got this and have been experimenting with it all day. I'm trying to get a specific tag out of the array but don't know how to do it. I'm interested in only applying damage to the enemies within. I know I could just leave it how it is and send a damage message to everything but I get the feeling that isn't very efficient. If you have any more help that would be great.

    Code (JavaScript):
    1. var center : Vector3 = transform.position;
    2.     var radius : float = reboundRadius;
    3.     var hitColliders = Physics.OverlapSphere(center, radius);
    4.    
    5.     for (var i = 0; i < hitColliders.Length; i++) {
    6.         hitColliders[i].SendMessage("AddDamage");  
    7.     }
     
  5. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Just add this under the for loop :

    Code (CSharp):
    1. if(hitColliders[i].transform.tag == "Enemy") // or maybe hitColliders[i].tag
    2. hitColliders[i].SendMessage("AddDamage");
    It will check if the collider that is being hit has the tag Enemy and send message to it if so.
     
  6. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    Why send message over GetComponent to properly calling the method, especially since you would prolly want add damage to receive some data such as the amount of damage and the damage type.
     
  7. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Actually if you think about this side, @passerbycmc is right, make your script work first, and then try reaching to component and calling a function over there instead of SendMessage, because SendMessage is expensive.
     
  8. PowerRidden

    PowerRidden

    Joined:
    Jun 24, 2014
    Posts:
    8
    Thanks guys, I made it work using the GetComponent! Here's the script that worked in case anyone is wondering.

    Code (JavaScript):
    1. var center : Vector3 = transform.position;
    2.     var radius : float = reboundRadius;
    3.     var hitColliders = Physics.OverlapSphere(center, radius);
    4.    
    5.     for (var i = 0; i < hitColliders.Length; i++) {
    6.         //print(hitColliders[i]);
    7.         if (hitColliders[i].transform.tag == "Enemy") {
    8.             hitColliders[i].GetComponent(Health).hp -= LbDamage;
    9.             //hitColliders[i].SendMessageUpwards("ApplyLBDamage", LbDamage, SendMessageOptions.DontRequireReceiver);
    10.         }
    11.     }
     
  9. Brominion

    Brominion

    Joined:
    Sep 30, 2012
    Posts:
    48
    You can also provide overlapSphere with a layermask. This may reduce the number of items in hitColliders (possibly ensuring that they are all enemies)