- Home /
How to destroy gameobjects with same tag using RaycastHit2D?
So, i have a problem with destroying a gameObject. I need to destroy a gameObjects with same tag that colliding with eachother but only when i press left mouse button and only ones that colliding with object that i clicked on. It sounds very complliceted...
 void Update()
     {
         PlayerInput();
     }
 
     void PlayerInput()
     {
         if (Input.GetMouseButtonDown(0))
         {           
             CastRay();
         }
     }
 
     void CastRay()
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit2D hit = Physics2D.Raycast(ray.origin, ray.direction, Mathf.Infinity);
         if (hit)
         {
                Destroy(hit.collider.gameObject);
         }
     }
 }
Code destroying only one gameObject when i click on it.
Answer by Fariborzzn · Jan 25, 2020 at 05:50 PM
mm if I understand you correctly it will help you
try this For 3D Project:
  void CastRay()
     {
         Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
         RaycastHit hit;
         bool checkforray = Physics.Raycast( ray , out hit , Mathf.Infinity );
 
         if (checkforray)
 
         {
             Collider[] hitColliders = Physics.OverlapSphere( ray.origin , 5f );
 
             foreach (var nearcollider in hitColliders)
             {
 
                 if (nearcollider.gameObject.tag==hit.collider.gameObject.tag)
 
                 {
                     Destroy(nearcollider.gameObject);
                 }
 
             }
 
 
         }
     }
try this for 2D Project:
    void CastRay()
     {
         RaycastHit2D ray = Physics2D.Raycast( Camera.main.ScreenToWorldPoint( Input.mousePosition ) , Vector2.zero );
         if (ray)
         {
             Collider2D[] hitColliders = Physics2D.OverlapBoxAll( ray.point , new Vector2(100 , 100 ),0f );
             foreach (var nearcollider in hitColliders)
             {
                 
                 if (nearcollider.gameObject.tag==ray.collider.gameObject.tag)
 
                 {
                     Destroy( nearcollider.gameObject );
                 }
 
             }
         }
     }
you can change the radius whatever you want and
it may work in 3D space but what about 2D? I'm working on the project in 2D space. I tried to edit your code for 2D but it doesn't want to work in "bool checkforray"`void CastRay() {
     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     RaycastHit2D hit ;
     bool checkForRay = Physics2D.Raycast(ray.origin, hit.point, $$anonymous$$athf.Infinity);
     if (checkForRay)
     {
         Collider2D[] hitColliders = Physics2D.OverlapCircleAll(ray.origin, 5f) ;
         foreach (var nearCollider in hitColliders)
         {
             if(nearCollider.gameObject.tag == hit.collider.gameObject.tag)
             {
                 Destroy(nearCollider.gameObject);
             }
         }
     }       
 }`
Am i doing something wrong? I can't understand how convert this code to work in 2D. But your code was very useful for me, thank you.
hey man I changed my answer now it will work in 2d too enjoy
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                