• 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

OnParticleCollision Question (Image included) Particle Decal Hanging off Edge 1 Answer

How to check if two objects collide without a collider 1 Answer

OnCollisionEnter not working with CharacterController 1 Answer

Rigidbody MoveTo considering collisions 1 Answer

Smooth movement for the block upon player collision,Smooth box movement 0 Answers


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