Search Unity

How do I get a group of gameObjects to "stick" with each other?

Discussion in 'Physics' started by Cremator, May 20, 2018.

  1. Cremator

    Cremator

    Joined:
    Dec 23, 2015
    Posts:
    11
    Hi guys! I made a building in Blender and shattered it with cell fracture addon, now I'm in Unity and I want my broken building's parts to keep stick to each other until a a huge force is applied to it, as a result my building should be able to collapse like a building instead of just exploding, I already tried fixed Joint and configurable joint but they all mess up... any ideas?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442
    maybe enable [x] Kinematic in rigidbody? (then disable it when they receive force or on explosion)
     
  3. Cremator

    Cremator

    Joined:
    Dec 23, 2015
    Posts:
    11
    Hi Mgear first of thanks for replying, I kind of figure out how to do what I want, my building is an Empty with a Rigidbody
    this Empty is composed of every chunks of the building which don't have rigidbody, this is how I manage to make them stick to each other.Then to make it break appart I had to the selected chunks a Rigidbody, which make them move independantly.

    However I get another problem, I can't add to my chunks Rigidbodies with the addComponent Method throught script, I read it could be due to other chunks colliders that prevent the rigidbody from appearing because of not enougth space or something, but when I add a bunch of Rigidbody in game to the chunks I get exactly what I want.

    So how do I add Rigidbody to my chunck throught script now?

    EDIT: I kinda figure out what to do thanks to mgear's answer who led me in another solution,
    I get every chunks that are colliding with the chunk carrying the script and create FixedJoints between the chunk and the colliding chunks in order to stick those pieces together, here is my code :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;  
    4.  
    5. public class rb : MonoBehaviour
    6. {
    7.  
    8.    
    9.     public List<GameObject> currentCollisions = new List<GameObject>();
    10.     public List<FixedJoint> possibleJoints = new List<FixedJoint>();
    11.     public List<Rigidbody> ConnectedChunks = new List<Rigidbody>();
    12.     private GameObject go;
    13.     public int id;
    14.  
    15.     void Start()
    16.     {
    17.         go = this.gameObject;
    18.     }
    19.  
    20.     void OnCollisionEnter(Collision col)
    21.     {
    22.      
    23.         currentCollisions.Add(col.gameObject);
    24.         id = currentCollisions.Count - 1;
    25.  
    26.  
    27.         go.AddComponent<FixedJoint>();
    28.  
    29.         possibleJoints.Add(col.gameObject.GetComponent<FixedJoint>());
    30.  
    31.         ConnectedChunks.Add(col.gameObject.GetComponent<Rigidbody>());
    32.  
    33.         for (int i = 0; i <= id; i++)
    34.         {
    35.             possibleJoints[i].connectedBody = ConnectedChunks[i];
    36.         }
    37.  
    38.  
    39.     }
    40.  
    41. }
    I need to specify the connected body (Rigidbody) for each fixedjoints created in my gameObject, I however when looking at the inspector of my GameObject in runtime, none of my connected body are filled... I know it's because of the for loop but what is the problem?
     
    Last edited: May 21, 2018
  4. Cremator

    Cremator

    Joined:
    Dec 23, 2015
    Posts:
    11
    Anybody else?