- Home /
How to move a group of objects with rigidbodies together?
I know this has been discussed many times and is considered to be bad practice.
What I am wanting to do is click and drag multiple objects that already have rigidbodies. These are currently moved by parenting them to an empty game object with rigidbody, that is moved via spring joint to another empty game object moved by transform.
Currently I have:
[dragging object] <- Rigidbody (kinematic), moved via transform
|
| <- spring Joint
|
[empty parent obj] <- Rigidbody (not kinematic / rotation locked) moved via spring joint
/ | \
/ | \ <- Parenting
/ | \
[child] [child] [child] <- Multiple child Rigidbodies (kinematic)
I want to be able to have all of the children drag in unison and this current setup DOES work, except it doesn't have collisions as a group (ie, if child to the left runs into an immovable wall, the whole group doesn't stop)
I know about the compound collider thing, that if I were to dynamically remove all child rigidbodies that would work the way I want. But many of the child objects (before moving) will have joints, forces, etc that would make frequently removing/adding rigidbodies a pain.
I've tried a few methods using joints instead of parenting, but even with adjusting the spring/joint settings, the objects seem to bounce, spin, etc in a way I don't want.
Is there any good way to move a group of separate objects as a collective without removing the rigidbodies?
Answer by mdolnik · Aug 20, 2013 at 09:59 PM
Thanks 'whydoidoit'... I ended up going with a method of using ConfigJoints instead of parenting, with the values tweaked... for anyone's future reference this is what I ended up with:
[Spring Object] <- Rigidbody (kinematic), moved via transform
|
| <- spring Joint
|
[Dragged Obj] <- Rigidbody (not kinematic / rotation locked) moved via spring joint
/ | \
/ | \ <- Config Joint(s) [locked]
/ | \
[child] [child] [child] <- Multiple child Rigidbodies
Spring Object:
Rigidbody: Mass 1 | Drag 0 | AngDrag 0.05 | Grav false | Kinematic true
Spring: Spring 40 | Damper 4 | Max Dis 0.01 | BreakForce 10
Dragged Obj:
Rigidbody: Mass 1 | Drag 15 | AngDrag 1 | Grav false | Kinematic false | FreezeRot XYZ
Config Joint(s): XYZMotion Locked | ProjectionMode Pos&Rot
Child Objects
Rigidbody: Mass 1 | Drag 0 | Kinematic False | Grav false
The only thing now is to do a OnJointBreak() check to re-init when the spring pulls too hard on a solid wall/floor and breaks the connection
Answer by whydoidoit · Aug 20, 2013 at 06:35 PM
A rigidbody specifies a collision group. You don't need rigidbodies on the [child] objects when they are being dragged, their colliders will be a compound group on the parent which should give you the effect you want.
If you sometimes need rigidbody components on the [child] objects then you will have to Destroy and AddComponent them when switching between modes of operation.
I mentioned:
But many of the child objects (before moving) will have joints, forces, etc that would make frequently removing/adding rigidbodies a pain.
This method is the last resort that I want to go to, because that would require keeping track of all of the connections that would get broken when removing the rigidbody.
Yep and I'm afraid that's all you've got unless you want to write the code to pass the collisions back up to the parent, perhaps using Send$$anonymous$$essageUpwards.
As I said, if you apply a rigidbody you've created a collision group of that item and anything with a collider and no rigidbody beneath it. If you add a rigidbody to a child collider then it will not be passing its collisions to the parent.
Darn... Does there happen to be any way to know which connections depend on the individual child rigidbody (ie: constant force, joints, etc), to restore later?
But aren't you child objects kinematic (and hence not having much to do with physics apart from collision) in your diagram? Perhaps I'm missing something....
If you have to, just create a script on the children that reacts to OnCollisionEnter and sends it upwards or just to the parent - presu$$anonymous$$g that is how you are detecting collision?