Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Destructible cube in air ?

Discussion in 'Physics' started by Quast, Dec 21, 2015.

  1. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I have a Destructible cube
    when the ball hit the cube...

    Good

    what i need, when i put the cube up, it should stay there also can be Destructible I remove the RB, it stay up but when i hit it nothing happen !

    any suggestion ?
     
  2. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Disable gravity on the cube and re-enable it when the ball collides with it using OnCollisionEnter().

    ?
     
  3. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Very good. BUT how to re-enable it with code ?
    cube.setactive(true); ?
     
  4. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    The cube have separated parts?
     
  5. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Add this script to your sphere and drag the cube parent transform into the public field "CubeParent" in the inspector.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SphereScript : MonoBehaviour
    5. {
    6.     public Transform CubeParent;
    7.     private Rigidbody[] cubeBodies;
    8.  
    9.     void Start ()
    10.     {
    11.         cubeBodies = CubeParent.GetComponentsInChildren<Rigidbody>();
    12.  
    13.         foreach (Rigidbody cb in cubeBodies)
    14.         {
    15.             cb.useGravity = true;
    16.             cb.isKinematic = true;
    17.         }
    18.     }
    19.  
    20.     void OnCollisionEnter()
    21.     {
    22.         foreach (Rigidbody cb in cubeBodies)
    23.         {
    24.             cb.isKinematic = false;
    25.         }
    26.     }
    27. }
     
    Last edited: Dec 22, 2015
  6. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    The code is not working, but i change the...
    Code (CSharp):
    1.             foreach (Rigidbody cb in cubeBodies)
    2.             {
    3.                 cb.useGravity = false;
    4.                 cb.isKinematic = false;
    5.             }
    it work but if i hit it every part go every where like in space ((really cool :D)). i want it all parts fail down.
    How to fix this ?
     
    Last edited: Dec 24, 2015
  7. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    That doesn't make any sense. Are you sure you dragged the cube parent into the field in the inspector?

    Does your sphere have a non kinematic rigidbody, and how are you moving it?

    What is the exact behavior you would like from the cube?

    It seems English is not your native language but you need to make an effort to reply as clearly and concisely as possible. Then I or someone else can help you more effectively.
     
  8. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Yes. this cube i make it on blender and then drag it to my scene. when i attached the C# script on my ball i drag the cube into CubeParent cell.

    -


    I move it by keyboard arrow.
    the cube should stay there in the air. when i hit it with any thing, ball or bult, all parts of the cube fail down //to gravity source//. ((the code you give to me it running really good if i'm working on a game not using gravity))
    You right. I'm not a English speaker. I will do my best to explain things in more clear way.
     
  9. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    I just made a test project with the same setup you have and the script I gave you works fine. Changing the script like you did is wrong.

    How are you moving the sphere rigidbody? By using addforce()?

    My guess is you aren't and so are maybe not getting an OnCollision call.

    Just try putting the sphere above the cube and let gravity automatically make it fall and hit the cube, then the cube should fall. Works for me.
     
  10. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    Thank you BBG for you help and i really appreciate it.
    I don't want to be look like a lier. I upload my scene and please check it.
    http://www.filedropper.com/3dworld
    http://www.megafileupload.com/qIbR/3d_world.zipf

    Just change the last thing in the code to this and see:
    Code (CSharp):
    1.    foreach (Rigidbody cb in cubeBodies)
    2.             {
    3.                 cb.useGravity = false;
    4.               //  cb.isKinematic = false;
    5.             }
     
  11. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    OK downloaded it. You have not got the script in place that I posted above. Change it to exactly that and it works.
     
  12. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I really don't know what to say (my eyes hurt me). I do exactly what you said, where is my mistake i don't know !!
    This is something simple, How if i was asking for something bigger ?!
    Please BBG, upload your file.
     
  13. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Just use the script in post 5. Drop the ball on the cube and you will see it works. I have done it myself in your project.

    If you keep changing the script it's not going to work. Just paste it over the one you have.
     
  14. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
  15. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I think you didn't understand. The file you upload didn't solve the problem.
    Have you note what happen to the Cube when the ball teach the plan or the wall ?


    If you played CB. you will find places where boxes are stock in the air. if you want what is inside it, you have to smash it which mean teach it. If you run under or around it nothing will happen to the box. This is what i need, If the ball hit the cube, every part of the cube blow-up.
     
  16. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Well yes, i just gave you a very basic script. You would have to filter out the other collisions with layers or something. You need to learn how to script so you can do these basic things yourself.

    Now you are more clear on what you want, I will create a better script that goes on the cube instead of the sphere.

    When the cube is hit do you want to destroy it after it falls down?

    While you wait add a box trigger collider to the cube parent.
     
  17. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    I will do my best. I learned some basic and i do really want to continue learning. but some time like this case you need to ask and i got more than what i need. ((space cube ;)))
    Thank you so much for that.
    NO. when i hit the cube only. no need to wait it to fall down and then destroy.
    OK.
     
  18. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Ok a new script which goes on the cube. You need a trigger collider on the cube parent which matches the shape of your cube. You will probably have to remake this because right now your parents pivot is off to the side and rotated which is really bad. it should be at the centre of the cube and not rotated. Then adding a box collider is easy.

    If you just want to destroy the cube straight away I don't know why you have all the pieces. Anyway this code should make it fall when hit then destroy after 3 seconds. Change the time of 'timeToDestroy' if you want it different.

    I also added a line where you could add a force to each cube piece so it explodes slightly different each time. But if you want to do that I will let you figure that one out :)

    Merry Christmas!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CubeScript : MonoBehaviour
    5. {
    6.     private Rigidbody[] cubeBodies;
    7.     private float timeToDestroy = 3.0f;
    8.  
    9.     private void Start()
    10.     {
    11.         cubeBodies = transform.GetComponentsInChildren<Rigidbody>();
    12.         foreach (Rigidbody cb in cubeBodies)
    13.         {
    14.             cb.useGravity = true;
    15.             cb.isKinematic = true;
    16.         }
    17.     }
    18.  
    19.     private void OnTriggerEnter()
    20.     {
    21.         foreach (Rigidbody cb in cubeBodies)
    22.         {
    23.             cb.isKinematic = false;
    24.             // add a randomized force to each Rogidbody here if you want them to explode instead of just falliing?
    25.         }
    26.         Destroy(this.gameObject, timeToDestroy);
    27.     }
    28. }
     
    Last edited: Dec 25, 2015
  19. Quast

    Quast

    Joined:
    Jul 5, 2015
    Posts:
    560
    You didn't got it yet. The code running fine but it not give me the result.
    All things i done it as you said. But still not give me what i asking for.
    Did you tried the code ? with me, If i didn't hit the ball the cube will destroy anyway !! also the "isKinematic", will not change when the ball hit box collider !!! another thing. I need this cube to stay on the air, so why when start the game the gravity is true ?
    Code (CSharp):
    1. cb.useGravity = true;

    What you mean by destroy is vanish or hide the cube from the scene. what i need is transfer the cube from ONE PIECE to 8 different pieces. these pieces is inside the cube as child. another example to make it more clear. Look at this Object that car will hit it.



    This what should happen to the cube when i hit it. even if it was above the ground.
    Good idea. This will make no similarity between cubes. I will work on this just after finishing from the damn cube. You give me things that help me to improve my game. yes, i didn't ask for it but maybe i will ask for it later. Thank you BBG.
     
  20. Fu11English

    Fu11English

    Joined:
    Feb 27, 2012
    Posts:
    258
    Ah okay. You need to put the cubeparent and children on two seperate layers and put the sphere on a layer which can collide with just the parent. I forgot that bit!

    I set gravity to true just incase it isn't already. isKinematic = true will keep it in the air. Then when it is triggered we set isKinematic to false which will make it fall.
     
  21. pauloaguiar

    pauloaguiar

    Joined:
    May 13, 2009
    Posts:
    700
    To my understanding, what you need and what you want, is when it collides with an object explodes into several parts.In my view, it is the best solution for your project.

    In "damageParts" script is the object you want to separate itself into several parts.
    It requires another script "DestroyTimeScript" to destroy the parts of the scene, not to accumulate waste in the scene and better performace.
    "damageParts Script"
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var standardHP : int = 50;
    4. private var negativeHP : int = 0;
    5. var currentArrayValue : int = 23; // Add curresponding body parts array
    6. public var bodyParts : Transform[] = new Transform[4]; // standard Array value.
    7.  
    8. var smoke : GameObject; // if explosion or other FX particles or item.
    9.  
    10. private var colliding : boolean = true;
    11.  
    12. function Update ()
    13. {
    14. //For testing.
    15.     if (Input.GetKey(KeyCode.P))
    16.     {
    17.         standardHPealth ();
    18.     }
    19. }
    20.  
    21. function OnTriggerEnter(col : Collider) // Or colliderEnter you decide.
    22. {
    23.     if (colliding)
    24.     {
    25.         standardHPealth ();
    26.     }
    27. }
    28. //****************************************************** health ******************************
    29. function standardHPealth ()
    30. {
    31.     standardHP --;
    32.    
    33.     if (standardHP <- 0)
    34.     {
    35.         standardHP = 0;
    36.     }
    37.    
    38.     if (standardHP <= 20)
    39.     {      
    40.         // Call smoke fx or item ,other.
    41.         smoke.gameObject.SetActive(true);
    42.     }
    43.                        
    44.     if (standardHP == 0)
    45.     {  
    46.         standardHP = 0;
    47.        
    48.         if (standardHP != null)
    49.         {
    50.             AddPhysics();
    51.             colliding = false;          
    52.         }  
    53.         Destroy (gameObject);
    54.     }
    55. }
    56.  
    57. //********************************************************************************************************
    58. function AddPhysics()
    59. {
    60.     if (colliding)
    61.     {
    62.         for (var i : int = 0; i < currentArrayValue; i++)
    63.         {
    64.             bodyParts[i].gameObject.SetActive(true);
    65.             bodyParts[i].gameObject.AddComponent(DestroyTimeScript);
    66.             bodyParts[i].transform.DetachChildren();
    67.             bodyParts[i].transform.parent = null;              
    68.                
    69.             bodyParts[i].gameObject.AddComponent(Rigidbody).interpolation = RigidbodyInterpolation.Interpolate;  
    70.             bodyParts[i].gameObject.GetComponent(Rigidbody).collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
    71.        
    72.             bodyParts[i].gameObject.AddComponent(MeshCollider).convex = true;
    73.         }
    74.     }  
    75. }
    "DestroyTimeScript Script".
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var destroyAfter : float =15;
    4. private var utimer : float;
    5.  
    6. function Update () {
    7.  
    8.     utimer += Time.deltaTime;
    9.    
    10.     if (destroyAfter <= utimer)
    11.     {
    12.         Destroy(gameObject);
    13.     }
    14.  
    15. }