Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DayQ · Jan 25, 2020 at 12:38 PM · destroycollider2dcollision2draycasthit2d

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DayQ · Jan 25, 2020 at 09:04 PM 0
Share

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.

avatar image Fariborzzn DayQ · Jan 25, 2020 at 10:49 PM 0
Share

hey man I changed my answer now it will work in 2d too enjoy

avatar image DayQ Fariborzzn · Jan 25, 2020 at 11:53 PM 0
Share

Thank you very much! You are breathtaking!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnCollisionEnter is not working for me!! 1 Answer

Is there a convenient way to check if two gameobjects are touching BUT NOT overlapping? 2 Answers

I have problem destroying my objects 1 Answer

How to make CircleCast work properly with multi vertex EdgeCollider2D 0 Answers

Prevent objects from pushing eachother on collide 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges