Search Unity

Destroy within a certain radius?

Discussion in 'Scripting' started by Deleted User, Dec 1, 2010.

  1. Deleted User

    Deleted User

    Guest

    Hello. When a condition happens, I want to destroy all objects with a certain tag that are within a certain radius of another object immediately, how would I do this? Thank you
     
  2. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    Try something like this:

    Code (csharp):
    1.  
    2. var explosionPosition = transform.position;
    3. var explosionRadius: float = 10.0;
    4. var colliders : Collider[] = Physics.OverlapSphere(explosionPosition, explosionRadius);
    5. foreach (var col in colliders)
    6.     {
    7.       if (col.collider.tag == "enemy")
    8.       {
    9.         Destroy(col.collider.gameObject);
    10.       }
    11.     }
    12.  
    You have to check the JavaScript format.
     
  3. Deleted User

    Deleted User

    Guest

    I know this is a noob thing to say, but how would I implement that script? I want to destroy the enemies in the radius after the player collides with that an enemies, so would it be something like this?

    Code (csharp):
    1. var explosionPosition = transform.position;
    2. var explosionRadius: float = 10.0;
    3. var colliders : Collider[] = Physics.OverlapSphere(explosionPosition, explosionRadius);
    4.  
    5. function OnTriggerEnter (other : Collider) {
    6.     if (other.gameObject.CompareTag ("Enemy")) {
    7.        Stats.Life -=1;
    8. foreach (var col in colliders)
    9.     {
    10.       if (col.collider.tag == "Enemy")
    11.       {
    12.         Destroy(col.collider.gameObject);
    13.       }
    14.     }
    15.     }
     
  4. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    My original code is intended for attaching to an explosion game object, such as grenade. Therefore, just attached the code to a explosion object and any objects within that radius you should destroy.

    Now, your code is a trigger. A trigger is fired when one game object collided with another game object. In this case, a player can only collide with one enemy at a time. How do you expect to kill all enemies with a radius? That does not make a lot sense in term of visual.

    Does the player have some kind of power, such a magic?
     
  5. Deleted User

    Deleted User

    Guest

    Okay. I actually want to use this code to help with the player respawn process. When the player gets hit, I want to destroy all objects that have the "Enemy" tag that are surrounding the player.
     
  6. mightymao

    mightymao

    Joined:
    Oct 21, 2009
    Posts:
    108
    OK, if you still insisted. I think that the following script will do what you want:

    1) attach a collider to each enemy and check mark Is Trigger property
    2) attach the following script to each enemy

    I hope that you create an enemy prefab

    C#
    Code (csharp):
    1.  
    2. public float radius = 10.0f;
    3.   // Use this for initialization
    4.   void Start()
    5.   {
    6.  
    7.   }
    8.  
    9.   void OnTriggerEnter(Collider other)
    10.   {
    11.     if (other.gameObject.CompareTag("Player"))
    12.     {
    13.       Stats.Life -=1;
    14.       print("collided");
    15.       Collider[] colliders = Physics.OverlapSphere(other.gameObject.transform.position, radius);
    16.       foreach (Collider col in colliders)
    17.       {
    18.         if (col.tag == "Enemy")
    19.         {
    20.           Destroy(col.gameObject);
    21.         }
    22.       }
    23.     }
    24.   }
    25.   // Update is called once per frame
    26.   void Update()
    27.   {
    28.  
    29.   }
    30.  
    JavaScript
    Code (csharp):
    1.  
    2.  var radius: float = 10.0;
    3.   // Use this for initialization
    4.   function Start()
    5.   {
    6.  
    7.   }
    8.  
    9.   function OnTriggerEnter(other: Collider)
    10.   {
    11.     if (other.gameObject.CompareTag("Player"))
    12.     {
    13.       Stats.Life -=1;
    14.       print("collided");
    15.       var colliders: Collider[]  = Physics.OverlapSphere(other.gameObject.transform.position, radius);
    16.       for (var col: Collider in colliders)
    17.       {
    18.         if (col.tag == "Enemy")
    19.         {
    20.           Destroy(col.gameObject);
    21.         }
    22.       }
    23.     }
    24.   }
    25.   // Update is called once per frame
    26.   function Update()
    27.   {
    28.  
    29.   }
    30.  
     
    ahunaxi likes this.
  7. Deleted User

    Deleted User

    Guest

    Whats a prefa...nah im just kidding :)
    Thanks for your help.