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

Instantiate more prefabs in OnCollisionEnter function.

Discussion in 'Scripting' started by Harry64, Aug 7, 2013.

  1. Harry64

    Harry64

    Joined:
    Jan 21, 2013
    Posts:
    18
    I am really really sorry for asking this but I am not a very skilled coder. just started this year with java coding.

    I have written this code here that does spawn a prefab when the object hits something hard enough. and the script applys the velocity of the original object to the spawned object and destroys the original object. like if a crate is falling down on the floor and disappears and then there spawns something else.

    but my problem is that I want to have debris there. so like a plate is falling on the floor and breaks into little pieces.

    I have modeled a plate and a broken plate with 5 pieces and now I realize that my script can only spawn 1 prefab only....

    can someone tell me please how to rewrite this code correct so that I can store more brokenObjects in this code with an array and that then every instantiated brokenObject gets the velocity from this object before it gets destroyed?

    Code (csharp):
    1.  
    2. #pragma strict
    3. var canBreak : boolean = false;
    4. var brokenObject : Transform;// this is the Transform that needs to get changed to Transform[] .
    5. var breakForce : float;
    6. var backBounceForce : float = 1;
    7.  
    8.  
    9. function OnCollisionEnter (collision : Collision)
    10. {
    11.     if(canBreak == false)
    12.         return;
    13.  
    14.     if (collision.relativeVelocity.magnitude > breakForce)
    15.     {
    16.         var object_to_destroy = gameObject;
    17.         var instantiated_object = Instantiate (brokenObject, transform.position, transform.rotation);
    18. // here is then the problem when I try to apply more prefabs into the Transform array from above and I dont know
    19. //how to change it so that it works with the code down there... :-(
    20.        
    21.        
    22.         var vel = -Vector3.Reflect(collision.relativeVelocity * backBounceForce, collision.contacts[0].normal);
    23.         //We take the incoming velocity, flip it over the normal of the contact, then flip the direction so it bounces back.   
    24.         var ang_vel = object_to_destroy.rigidbody.angularVelocity;
    25.  
    26.         //apply the velocity to the prefab.
    27.         instantiated_object.rigidbody.velocity = vel;
    28.         instantiated_object.rigidbody.angularVelocity = ang_vel;
    29.  
    30.         Destroy(object_to_destroy);
    31.     }
    32. }
     
  2. KyleOlsen

    KyleOlsen

    Joined:
    Apr 3, 2012
    Posts:
    237
    This should do it:

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var canBreak : boolean = false;
    5. var brokenObjects : Transform[];
    6. var breakForce : float;
    7. var backBounceForce : float = 1;
    8.  
    9. function OnCollisionEnter (collision : Collision)
    10. {
    11.     if(canBreak == false)
    12.         return;
    13.  
    14.     if (collision.relativeVelocity.magnitude > breakForce)
    15.     {
    16.         var vel = -Vector3.Reflect(collision.relativeVelocity * backBounceForce, collision.contacts[0].normal);
    17.         var ang_vel = rigidbody.angularVelocity;
    18.  
    19.         for(var i = 0; i < brokenObjects.Length; i++)
    20.         {
    21.             var instantiated_object = Instantiate (brokenObjects[i], transform.position, transform.rotation);
    22.             instantiated_object.rigidbody.velocity = vel;
    23.             instantiated_object.rigidbody.angularVelocity = ang_vel;
    24.         }
    25.  
    26.         Destroy(gameObject);
    27.     }
    28. }
    29.  
    (Note: I'm a C# programmer, not sure if this will actually compile.)
     
  3. Harry64

    Harry64

    Joined:
    Jan 21, 2013
    Posts:
    18
    yes it works thank you... but now it will spawn all at the same point.....and that dont look good....
    if I would put all into a empty then it would spawn it in the desired position...

    can I somehow tell the script to add the force to the next children but not to the children of the children because there would be just emptys with Audiosources?

    like
    empty--> child
    --> child
    --> child
    --> .... and so on...

    I need to go to work now so I cannot test this now no myselfe right now....

    that would be awesome from you ^^

    ----------------------------------EDITED------------------------------

    I have managed to get it to work like I want. now it works also if everything is in a parent.

    here is my script. btw maybe I change it a bit in the future so I could put more prefabs with children inside like if I would break a vase and then a key is also spawned from the vase. ^^

    Code (csharp):
    1. #pragma strict
    2.  
    3. var canBreak : boolean = false;
    4. var brokenObject : Transform;
    5. var breakForce : float;
    6. var backBounceForce : float = 1;
    7.  
    8. function OnCollisionEnter (collision : Collision)
    9. {
    10.     if(canBreak == false)
    11.         return;
    12.  
    13.     if (collision.relativeVelocity.magnitude > breakForce)
    14.     {
    15.         var vel = -Vector3.Reflect(collision.relativeVelocity * backBounceForce, collision.contacts[0].normal);
    16.         var ang_vel = rigidbody.angularVelocity;
    17.        
    18.  
    19.         var instantiated_object = Instantiate (brokenObject, transform.position, transform.rotation);
    20.         var children = instantiated_object.GetComponentsInChildren.<Rigidbody>();
    21.  
    22.         for(var i = 0; i < children.Length; i++)
    23.         {
    24.             children[i].velocity = vel + Vector3((Random.value - 0.5),(Random.value - 0.5),(Random.value - 0.5));
    25.             children[i].angularVelocity = ang_vel + Vector3((Random.value - 0.5),(Random.value - 0.5),(Random.value - 0.5));
    26.         }
    27.  
    28.         Destroy(gameObject);
    29.     }
    30. }
     
    Last edited: Aug 11, 2013