- Home /
Best way to destroy a large object into smaller fragments?
NOTE: Just the logic I really need, I'm doing fine with the programming.
Alright, here is how I'm doing it atm. Simply I have a empty parent object, who has 8 smaller children which make the whole comet, the parent also has a simple rigidbody to move all children at the same rate. All child objects are set to Kinematic, so they can't bump and break before they are told to, making them seem fused/glued together as one large object. When this is hit I make the parent detach the children, remove itself and disable Kinematic physics on the child objects, so they can all fall on their own little trajectories.
This all works, except by doing this when the children are dropped the whole Comet "stutters". When the parent removes itself it also takes all gravity/forces with it and the children are allowed to use their own, which of course due to being Kinematic previously it is all effectly null. So it falls naturally as one, then it is hit and it effectly stops for a breif second, then the smaller bits gain speed and fall natrually apart.
So what is a more effective meathod? I have also tried a similar idea wit rather than using Kinematic locks to use X,Y,Z position and rotation constraints. A similar thing happens, or it falls apart before it is hit.
All suggestions and help would help me GREATLY!
I didnt want to post this as an answer, because its not quite what your looking for, but maybe it'll spark an idea. I'm doing something similar, except as a shattering glass ball. Ins$$anonymous$$d of having the pieces fall when the ball breaks, I instantiate the pieces when the ball is hit, give them velocity based on the balls movement, then destroy the glass ball. Again, I know its not a direct answer to your problem, but it seemed like a close enough situation that it might helpful. Good luck!
Thanks all, got it fixed FINALLY!
Simply passed on the velocity to the children when before they are detached.
For people in the future, here is the basic line you want to feed.
var vel = rigidbody.velocity;
Then give this to whatever, you must make sure that $$anonymous$$inematic is set to false before hand!
if (GameObject.Find("Chunk1") != null) { transform.Find("Chunk1").GetComponent(RoidAwake).enabled = true; transform.Find("Chunk1").rigidbody.is$$anonymous$$inematic = false; transform.Find("Chunk1").rigidbody.velocity = vel; }
Answer by Rennat · Aug 29, 2011 at 05:57 PM
The suggested way of doing this in Unity is to replace your game object with a "wrecked" one where your main gameobject may have one rigidbody your wrecked can look the same but instead be made of many rigidbody pieces. Similar to what you are doing now but more efficient.
With either method you'll need to transfer the force from one rigidbody to all the new smaller ones. I had to do this recently and I accomplished it by using the "wrecked" prefab method and calling the replacement with this script (on the real gameobject not the wrecked one):
c#
public GameObject wreckedPrefab; // set in editor
public void DoWreck () {
// instantiate the new object
GameObject wrecked = (GameObject)Instantiate(wreckedPrefab, transform.position, transform.rotation);
// transfer rigidbody force
for (Rigidbody body in wrecked.GetComponentsInChildren<RigidBody>()) {
body.AddForce(rigidbody.velocity, ForceMode.VelocityChange);
}
// get rid of the original
Destroy(gameobject);
}
Typed it from memory so there's probably a typo/error but you should be able to glean the method from it.
I also used `Rigidbody.AddExplosionForce` and attached smoke particle emitters to the small rigidbodies for my project.
Hope that helps.
Answer by genetixsparkz · Aug 29, 2011 at 06:04 PM
can't you save the parents current rigidbody forces and then apply those forces to the child to give it initial inertia?
I mean to me it sounds like all your saying is my smaller bits are starting out to slow, so you need it to start out with a higher speed, the most obvious to add is that since the object was actually already in motion you add the speed it was already moving at, since its course has basically been deflected by the shattering it should have retained alot of the speed it had.
edit: heh 2 people beat me to it, yea just save the parent force.