Create a new object after the collision of two others

Hi folks!

I need you opinion about this :slight_smile:

please see linked image

alt text

I need to create a new object from the collision of two identical objects, then make the size of the new one equal to the sum of the two objects sizes.

Any suggestions?

Tnx :slight_smile:

What you need to look at are:

  1. questions on instantiate
  2. questions about collisions
  3. the documentation on instantiate and collisions

Getting the sizes right is a matter of calculating their volumes and adding them together. If they're primitive type objects, it'll be easy enough; if not, it could get a little trickier (or just guess-timate it).

Edit: question is actually a little trickier than just destroy and instantiate. This code successfully detects collisions between two rigidbodies, destroys them, and instantiates only one larger object:

var spherePrefab : GameObject;
var willSpawn : boolean = true;

//set a flag depending on if there's already been a collision or not
function canSpawn(b: boolean){
    willSpawn = b;

function OnCollisionEnter(other:Collision){
    var othGO: GameObject = other.gameObject;

    //check the flag first so you don't do an unnecessary lookup
    if (willSpawn && ( == name || == name + "(Clone)")){ //assuming you're looking to compare two objects with the same names
        othGO.SendMessage("canSpawn", false);
        var newSphere: GameObject = Instantiate (spherePrefab, other.contacts[0].point, Quaternion.identity);
        newSphere.transform.localScale += Vector3(2,2,2);