- Home /
Create a new object after the collision of two others
Hi folks!
I need you opinion about this :)
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 :)
Answer by Chris D · Jul 31, 2011 at 10:14 PM
What you need to look at are:
- questions on instantiate
- questions about collisions
- 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){ print("collision"); var othGO: GameObject = other.gameObject;
//check the flag first so you don't do an unnecessary lookup
if (willSpawn && (othGO.name == name || othGO.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);
}
Destroy(gameObject);
}
Hi Chris thanks for the answer!
I just know some basics about istance anc colliders and my question was more about the initial approach.
For example it's better to have a listener outside objects that create a new one or just a script inside them?
I already check collision between the two objects and I make an instance if the collision is with an object with the same name and destroy the original...the problem is that the script is the same in each object so I'm getting...two new instances! :D
Ah, I see what you're after now. In that case it's not as straight forward, but still not bad. See my edited answer above.
The only issue I've run into with this code is that it starts running into issues with successive generations of instantiations. I added the alternate case of running into a clone (the third check in the if
statement) and that takes care of the second generation. It should be good enough to show you the general approach, though :D
Hey, Im sorry to bother you guys, especially on such an old post but I just wanted to ask if there was a way to me the sphereprefab move ins$$anonymous$$d of staying still when Instantiated. I would like there to add some force to it from the collision of the 2 smaller objects. I tried to have the variables as Rigidbodies but then one of the smaller objects doesnt get destroyed. I have a Rigidbody attached to both my sphereprefab and the smaller objects. Heres a sample of my slightly modified code from above : var EnergyBall : GameObject; var willSpawn : boolean = true; function canSpawn(b: boolean){ willSpawn = b; } function OnCollisionEnter(other:Collision){ var othGO: GameObject = other.gameObject; if (willSpawn && (othGO.tag == "Bullet" || othGO.tag == "Bullet" + "(Clone)")){ othGO.Send$$anonymous$$essage("canSpawn", false); var NewBall: GameObject = Instantiate (EnergyBall, transform.position, Quaternion.identity); } if (other.collider.tag == "Bullet") Destroy(gameObject); }
I cant get the "EnergyBall" to have any kind of force. I would use add force but that would have to be in a specific direction. I want to use the force of the two smaller objects to have an influence on how fast the EnergyBall goes and which direction it goes. Thanks for reading all this, heh. Its not really a comment but whatever, I guess. Sorry to bother you guys and have a nice day.
Your answer
