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 adelgeit · Feb 14, 2020 at 10:15 PM · collisioncollision detectioncubes

Diasble collision between exsisting gameObjects and newly created ones

alt text

I make a game where the robot should hit the cube and then the cube exploids to a lot of small cubes and gets destroyed after some time. The problem is that I do not know how to neglect the collison between newly generated small cubes and all other rigidbodies. I have tried to enable isTrigger for them but does not seem to work. Any ideas? The code is also included.

 private void OnCollisionEnter(Collision collision)
     {
         if (collision.collider.tag == "Obstacle")
         {
 
             Debug.Log("hit");
             Explosion();
         }
     }
      void Explosion ()
     {
         gameObject.SetActive(false);
         for (int x = 0; x < cubesAmount; x++)
         {
             for (int y = 0; y < cubesAmount; y++)
             {
                 for (int z = 0; z < cubesAmount; z++)
                     createPieces(x, y, z);
             }
         }
         Vector3 explPos = transform.position;
         Collider[] colliders = Physics.OverlapSphere(explPos, radius);
         foreach (Collider hit in colliders)
         {
            Rigidbody rb2 = hit.GetComponent<Rigidbody>();
             if (rb2 != null)
             {
                 rb2.AddExplosionForce(Random.Range(explosionMin, explosionMax), transform.position, radius);
             }
             if (hit.gameObject.name == "piece")
             {
                 //hit.isTrigger = true;
                 Destroy(hit.gameObject, 5f);
             }
         }
         
 
     }
     void createPieces(int x, int y, int z)
     {
         GameObject piece;
         piece = GameObject.CreatePrimitive(PrimitiveType.Cube);
         piece.transform.position = transform.position + new Vector3 (pieceSize * x , pieceSize * y, pieceSize * z); // setting position
         piece.transform.localScale = new Vector3 (pieceSize, pieceSize, pieceSize); //setting scale of the object
         piece.AddComponent<Rigidbody>();
         piece.GetComponent<Rigidbody>().useGravity = true;
         piece.GetComponent<Rigidbody>().mass = pieceSize;
         piece.GetComponent<Renderer>().material = material;
         piece.GetComponent<Collider>().isTrigger = true;
         piece.name = "piece";
 
     }
         
 }

2.png (44.6 kB)
1.png (37.2 kB)
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 mjc33 · Feb 14, 2020 at 10:24 PM

A simple approach would be to put each "piece" game object into a different layer during creation and disable collisions for that layer in the collision matrix in project settings:

Would just need to add this line of code before adding the Rigidbody component

  piece.layer = [int layer number];

eg.

 piece.layer = 3;



Comment
Add comment · Show 2 · 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 adelgeit · Feb 14, 2020 at 10:35 PM 0
Share

I have tried this out, I have put every piece to one separate layer called "pieces" and then removed layer collision, but still does not work... alt text

3.png (8.5 kB)
avatar image adelgeit · Feb 14, 2020 at 11:01 PM 0
Share

I have tried to use Physics.IgnoreLayerCollision and it worked, thank you

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

189 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 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

Collision Detection Issue – Player in the corners 1 Answer

brick does not detect collision although ball does 2 Answers

How do I fix this weird collision "bug"? 1 Answer

Handling collision with fast Lerp 1 Answer

Car wheel not detecting ground 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