Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 LeftyTwoGuns · Jul 22, 2013 at 01:26 AM · physicscolliderexplosion

Getting object to destroy on impact

I modeled a simple brick in Blender and then used Cell Fracture to create a new prefab that I instantiate when the brick is hit by a sphere to simulate the brick being destroyed.

The bricks are currently suspended in mid air (intentional) and they are destroyed on being hit by the sphere and the debris prefab is instantiated and falls, but the debris prefab doesn't break apart until it hits the ground or another object. I'd like the debris prefab to break apart as soon as it's instantiated to simulate the brick being broken apart by the sphere.

I'm new to Unity, so my best guess is that I need to use an Explosion Force to simulate the sphere breaking the brick? Because the debris prefab isn't being broken apart because technically it was never hit by the sphere? Or am I missing something else? Thanks!

Comment
Add comment · Show 1
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 LeftyTwoGuns · Jul 23, 2013 at 08:39 PM 0
Share

Ok, now I understand it! I haven't worked with ExplosionForce or collider points yet but I think I get their basic function. That particular script works perfectly, I just need to tweak the numbers and experiment to see what kind of secret sauce you can do with ExplosionForce.

I liked the simplicity of the first suggestion but this is a Breakout-style game so the bricks are suspended in mid-air. I'll try experimenting with it to try and find if there's a way to keep the debris prefab suspended but still be broken apart by the ball and act accordingly. I imagine the simpler approach would be easier on performance than having so many things be instantiated.

Thank you both again!

2 Replies

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

Answer by aldonaletto · Jul 22, 2013 at 03:52 AM

In fact, the original collision doesn't affect the debris because they were created after it. You should add a rigidbody and a collider to each one of the debris, and apply forces to make them break apart. A good solution is to child the debris to an empty object (the debris prefab) and use GetComponentsInChildren in order to get a list of all pieces, then apply the ExplosionForce to each one - like this (brick script):

 var debrisPrefab: GameObject; // drag the debris prefab here
 var force: float = 500;
 var radius: float = 15;
 
 function OnCollisionEnter(col: Collision){
   // create replacement pieces:
   var brokenBrick: GameObject = Instantiate(debrisPrefab, transform.position, transform.rotation);
   var pos = col.contacts[0].point; // get hit point
   // get a list with all rigidbodies in the broken brick object:
   var debris: Component[] = brokenBrick.GetComponentsInChildren(Rigidbody);
   // add explosion force to them according to their positions:
   for (var rb: Rigidbody in debris){ 
     rb.AddExplosionForce(force, pos, radius);
   }
   Destroy(gameObject); // destroy original brick
 }    

EDITED: This is the C# version (hope it's ok - the C# compiler is evil!):

   using UnityEngine;
   using System.Collections;
 
   public class ExplodeBrick : MonoBehaviour {
     public GameObject debrisPrefab; // drag the debris prefab here
     public float force = 500;
     public float radius = 15; // explosion force decreases to zero at this distance
     
     void OnCollisionEnter(Collision col){
       // create replacement pieces:
       GameObject brokenBrick = Instantiate(debrisPrefab, transform.position, transform.rotation) as GameObject;
       Vector3 pos = col.contacts[0].point; // get hit point
       // get a list with all rigidbodies in the broken brick object:
       Component[] debris = brokenBrick.GetComponentsInChildren<Rigidbody>();
       // add explosion force to them according to their positions:
       foreach (Rigidbody rb in debris){ 
         rb.AddExplosionForce(force, pos, radius);
       }
       Destroy(gameObject); // destroy original brick
     }    
   }
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 LeftyTwoGuns · Jul 22, 2013 at 11:25 PM 0
Share

I've been mainly working with C# so I'm having a bit of a problem translating this from JavaScript. Is it possible for someone to post these function in C#? ,I've been mainly working with C# so I'm having a bit of a problem translating this from JavaScript. Is it possible for someone to post these function in C#?

avatar image aldonaletto · Jul 23, 2013 at 01:27 PM 0
Share

Converting this code to C# isn't so trivial, since there are many pitfalls: GetComponentsInChildren is generic, for..in becomes foreach and you mast cast Instantiate to the correct type.

avatar image
1

Answer by robertbu · Jul 22, 2013 at 03:41 AM

There are multiple ways of doing most things in Unity. Add an explosion force to each fragment is one way. The strength of AddExplosionForce() is that it drops off linearly with distance. I don't think you need that here, so using AddForce() would work fine.

But let me suggest an alternate. Instantiate both the whole brick and the fragments at the same time. Make the whole brick a bit larger so that it hides the fragments. The whole brick would not have a collider. When any of the fragments gets hit, it can turn off the renderer for the whole brick (parent) exposing the fragments. That way the sphere is the thing that causes the fragments to fly creating breakage that better matches the sphere hit. You'll have to play with the mass of the sphere and the fragments to get the look you like.

Comment
Add comment · Show 1 · 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 LeftyTwoGuns · Jul 22, 2013 at 07:43 PM 0
Share

Thank you both for the answers! I'll mess around with both solutions.

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

16 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

Related Questions

My object falls through terrain. 8 Answers

Character Controller Pushes Car With Wheel Colliders? 0 Answers

Best collision detection method? 2 Answers

How do I detect current collisions without using onCollisionEnter? 0 Answers

Weapon passing through colliders (objects)? 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