- Home /
Add force to child
How do I add force to a group of child objects on a key press and then detach them so they can float freely in the direction they were launched?
The idea is to make something like breakable glass, where each piece of the object is actually held together by an empty and on a push of a button, the pieces have force applied and separate, giving the illusion of breaking apart.
Answer by Bampf · Apr 06, 2011 at 01:52 PM
You could start with each child object's rigidbody set to is-kinematic. When the parent object moves the children will follow.
Next: just before (or after?) applying the force turn off is-kinematic on all the children. You can un-parent them with one function call: Transform.DetachChildren. (Note: unparenting them is probably optional. According to the "Parenting" section of the Rigidbody docs, non-kinematic children will still react to forces on them. As long as the parent stays still it should work.)
There are various ways to apply the force. The first one to try is probably Rigidbody.ApplyExplosionForce because the same call for each child (same force and position args) will produce a varying effect depending on how far each piece is away from the center of the blast. But if that doesn't produce the desired effect, you could calculate the position and force differently for each child object. (For example, if the blast doesn't emanate outward from a single point, you might want to push the child objects mostly in the same direction, or collapse them inwards.)
Edit: I haven't tested this, but the code to detach the children after applying an explosive force to them centered at the parent's coordinates might look something like this, in a script attached to the parent:
for (var child : Transform in transform) {
child.rigidbody.AddExplosionForce(5.0, transform.position, 10.0, 2.0);
}
transform.DetachChildren(); // this line may be optional, as long as parent object is stationary
So apply force to the parent, then separate it, and they would still have the force of the parent?
I think not. A force on the parent will push it (and everything rigidly attached) in one direction. I'm almost certain you will need to apply the force to each of the children's rigidbodies.
I added an (untested) example to my answer. The numbers in the AddExplosionForce call would have to be tweaked for your needs.
Your answer
![](https://koobas.hobune.stream/wayback/20220613121057im_/https://answers.unity.com/themes/thub/images/avi.jpg)