Search Unity

Swapping solid GameObject for "shattered" prefab causes the pieces to blow up.

Discussion in 'Physics' started by Strom_CL, Feb 3, 2021.

  1. Strom_CL

    Strom_CL

    Joined:
    Nov 17, 2012
    Posts:
    115
    I have a very simple scene, with a rock on the ground. I have a harvesting script that removes health from it, and when it reaches zero I destroy the GameObject and instantiate a version that I shattered in Maya, and then I destroy the pieces after 5 seconds. Super simple right? Well, the new object pieces explode out from where they start, which is not what I expected. I would like them to just crumble where they are and not blow up like they do. I've watched a few other videos / tutorials on shattering objects and they all fall to the ground normally so I don't know what's going on. Any ideas/help would be greatly appreciated.

    Code (CSharp):
    1.  
    2. private void FixedUpdate()
    3. {
    4.     if (health <= 0)
    5.     {
    6.         SpawnShatteredRock();
    7.     }
    8. }
    9.  
    10. private void SpawnShatteredRock()
    11. {
    12.     Destroy(gameObject);
    13.     GameObject newRock = Instantiate(rock, transform.position, transform.rotation);
    14.     Destroy(newRock.gameObject, 5.0f);
    15. }

    This is what the Shattered prefab looks like for each broken piece:



    And this is what it looks like in game:

     
  2. Strom_CL

    Strom_CL

    Joined:
    Nov 17, 2012
    Posts:
    115
    Also wanted to add that if I just drag my shattered prefab into the scene, it crumbles as I would expect. I have an almost zero friction (0.1 dynamic and 0.1 static friction) material on each shattered piece.
     
  3. AlTheSlacker

    AlTheSlacker

    Joined:
    Jun 12, 2017
    Posts:
    326
    I'm guessing you have a mesh collider the same size as the pieces and that you instantiating the pieces with their colliders on top of each other. How about shrinking all your pieces just enough that they don't touch each other at spawn?
     
  4. Strom_CL

    Strom_CL

    Joined:
    Nov 17, 2012
    Posts:
    115
    I ended up working through it with someone from Discord. Turns out I was doing everything in FixedUpdate, so when I'd destroy the rock, then instantiate the shattered version the original was still there for a frame, thus the colliders would overlap and the new one would blow up.

    I found two fixes, first was disabling the collider on the original rock before instantiating and the second was to move the ShatterRock() call to a normal Update loop and then this doesn't require the collider to be disabled. I guess that's what I get for using FixedUpdate for the first time. :)
     
  5. Strom_CL

    Strom_CL

    Joined:
    Nov 17, 2012
    Posts:
    115
    Thread necro! Revisited this project recently, found the explosion issue happening in v2021.2.17f1, fix this time around was to enable Adaptive Force and up the Default Solver Velocity Iterations from 1 to 5 and shattered models spawn and crumple very close to what I would expect now.