Search Unity

gravity not pulling mesh down?

Discussion in 'Physics' started by ariusdb222, Apr 23, 2015.

  1. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Hey everyone, i managed to get the destructible buildings right:)
    Now i have a small problem though, can anyone look at the pics and tell me what you think?
    2015-04-23 09_38_52-Greenshot.jpg
    2015-04-23 09_53_11-Greenshot.jpg

    So it all works well and whatever.. but then, just a few pieces stay connected where they should fall...
    Also is the way i'm doing this right or not even close?
    I just started working with the physics...
    Thanks for any replies!
     
  2. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    249
    Those rigidbodies are asleep; wake them up by calling WakeUp()
     
  3. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    Thanks!, i'v read about the is kinematic stuff...
    So would it not be a lot easier to make the building kinematic, then on collision above a certain strength it will break the parts that are affected?

    Also should be better for performance or am i wrong?
    And let me know if there is another way to keep the building from collapsing without having a joint on every object/fracture
    cause theres like 500 - 2500 on my building....
     
  4. andrew-lukasik

    andrew-lukasik

    Joined:
    Jan 31, 2013
    Posts:
    249
    Yes, switching is kinematic is certainly faster than active joints.
    But I think you should rethink this; rendering 500+ meshes alone will take too much CPU's time. You need some way to merge all meshes while they're at rest.
    I had similar problem last week and simplest way to do this I've found so far is to disable all MeshRenderers on Awake and make one new MeshRender (per building for example) that merges all those meshes to one. And then when one of rigidbodies needs to be detached - I'm enabling it's MeshRender and update merged mesh while excluding this activated piece.

    Not sure if it works (I edited out some game-specific stuff) but still you may find some useful code here. To use this add this component to parent gameObject and write small script for each of them that detaches themselves transform.parent=null; and calls GetComponentInParent<DestructibleStructureOptimizer>().RequestMeshUpdate();

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent( typeof(MeshFilter) )]
    5. [RequireComponent( typeof(MeshRenderer) )]
    6.  
    7. public class DestructibleStructuresOptimizer : MonoBehaviour {
    8.  
    9.     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    10.     // FIELDS & PROPERTIES ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    11.  
    12.     // SETUP //
    13.  
    14.  
    15.  
    16.     // RUNTIME //
    17.  
    18.     //
    19.     [SerializeField] private MeshFilter _meshFilter = null;
    20.     //
    21.     [SerializeField] private MeshRenderer _meshRenderer = null;
    22.     //
    23.     private MeshFilter[] meshFilterComponents;
    24.     //
    25.     CombineInstance[] combineInstances;
    26.     //
    27.     private bool _updateMeshRequested = false;
    28.  
    29.  
    30.  
    31.     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    32.     // MONOBEHAVIOUR MESSAGES ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    33.  
    34.     // AWAKE //
    35.     void Awake () {
    36.         UpdateMeshProcedure();
    37.     }
    38.  
    39.     #if UNITY_EDITOR
    40.     // ON VALIDATE //
    41.     private void OnValidate () {
    42.         if( _meshFilter==null ) _meshFilter = GetComponent<MeshFilter>();
    43.         if( _meshRenderer==null ) _meshRenderer = GetComponent<MeshRenderer>();
    44.     }
    45.     #endif
    46.  
    47.     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    48.     // PRIVATE FUNCTIONS /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    49.  
    50.     // UPDATE MESH PROCEDURE //
    51.     private void UpdateMeshProcedure () {
    52.         _meshRenderer.enabled = false;
    53.         //
    54.         meshFilterComponents = GetComponentsInChildren<MeshFilter>();
    55.         combineInstances = new CombineInstance[meshFilterComponents.Length];
    56.         for( int i = 0 ; i<meshFilterComponents.Length ; i++ ) {
    57.             meshFilterComponents[i].GetComponent<MeshRenderer>().enabled = false;
    58.         }
    59.         _meshRenderer.material = meshFilterComponents[0].GetComponent<MeshRenderer>().sharedMaterial;
    60.         //
    61.         for( int i = 0 ; i<meshFilterComponents.Length ; i++ ) {
    62.             if( meshFilterComponents[i].gameObject.activeSelf ) {
    63.                 combineInstances[i].mesh = meshFilterComponents[i].sharedMesh;
    64.                 combineInstances[i].transform = transform.worldToLocalMatrix*meshFilterComponents[i].transform.localToWorldMatrix;
    65.             }
    66.             else {
    67.                 combineInstances[i].mesh = new Mesh();
    68.             }
    69.         }
    70.         _meshFilter.mesh.CombineMeshes( combineInstances );
    71.         //
    72.         _meshRenderer.enabled = true;
    73.         _updateMeshRequested = false;
    74.     }
    75.  
    76.     // UPDATE IN PROGRESS //
    77.     private IEnumerator UpdateInProgress () {
    78.         yield return new WaitForEndOfFrame();
    79.         //
    80.         UpdateMeshProcedure();
    81.     }
    82.  
    83.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    84.     // PUBLIC FUNCTIONS ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    85.  
    86.     // REQUEST MESH UPDATE //
    87.     public void RequestMeshUpdate () {
    88.         if( _updateMeshRequested==false ) {
    89.             _updateMeshRequested = true;
    90.             StartCoroutine( UpdateInProgress() );
    91.         }
    92.     }
    93.  
    94.  
    95. }

    PS: this uses one material only (on all pieces) but it can be upgraded to handle more.
     
    Last edited: Apr 24, 2015
  5. ariusdb222

    ariusdb222

    Joined:
    Jan 25, 2015
    Posts:
    88
    wow thanks!:D

    So i figured out why those parts were staying in place.....
    Seems to only be doing that when im using the fixed joints...

    Is the fixed joints the right thing to use? cause my buildings fall apart without them...
    Maby i messed something up that the buildings fall apart, but i think its just the gravity?

    Thanks for the help!