How to Make Two Objects Stick Together?

I have two objects, each with its own rigidbody and collider, that move around the scene under physics control, bumping into things and bouncing around. But when they collide with each other, these two objects need to stick together and continue to move around as a single object with a single center of gravity.

To do this, I created a third, empty object that has a rigid body (and nothing else) attached. When the two objects collide, I set the position of the third object to the midpoint between the two, make the two objects children of the third, make them non-kinematic, and make the third object kinematic, then transfer the velocity of the two to the third one:

puckDoubleObj.transform.position = Vector3.Lerp(puckObj[0].transform.position, puckObj[1].transform.position, 0.5);
puckObj[0].transform.parent = puckDoubleObj.transform;
puckObj[1].transform.parent = puckDoubleObj.transform;
puckObj[0].rigidbody.isKinematic = true;
puckObj[1].rigidbody.isKinematic = true;
puckDoubleObj.rigidbody.isKinematic = false;
puckDoubleObj.rigidbody.velocity = puckObj[0].rigidbody.velocity + .puckObj[1].rigidbody.velocity;

This works as far as motion of the stuck objects is concerned. But after they collide and stick together, they no longer collide with anything else. The stuck object goes through walls instead of bouncing off like the two separate objects did.

Am I doing something wrong, or is there a better way to do this? Why would collisions no longer work when I’ve not changed the colliders? Does it have something to do with there being three rigidbodies attached to a single object/children, even though two of them are kinematic?

I am guessing it is because the parent has no collider.

I believe by setting the two sing objects to Kinematic is messing with the parents non-kinematic. Perhaps just remove the Rigidbody component from the two singles instead of making them Kinematic.

If I attach a collider to the third object, then they stop after the collision. They hit, stick, then don’t move after that.

I thought I could disable the rigidbodies, but that’s not an option, which is why I ended up setting them to kinematic. How can I remove the rigidbodies from the single objects after the collision? I could destroy them, but would later need to get them back when the objects eventually become unstuck.

Just tried destroying the individual objects’ rigidbodies on collision, and that causes them to go crazy. The two stuck objects started moving around, apparently at random, appearing to collide with nothing and bounce around.

You can disable rigidbodies.

gameObject.GetComponent().enabled = false;

I believe something that should do the trick for enabling/disabling the rigidbody.

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-isKinematic.html

First line in the documentation - kinematic rigidbodies are not affected by collisions.

No, that doesn’t work. This code:

puckObj[0].rigidbody.enabled = false;

produces this error:

puckObj[0].gameObject.GetComponent().enabled = false;

‘enabled’ is not a member of ‘UnityEngine.Rigidbody’

I understand. So, even though the two single objects still have their colliders, because their rigidbodies are set to kinematic, they no longer detect collisions. Makes sense.

So then why doesn’t destroying their rigidbodies when thy first get stuck work? If they have colliders without a rigidbody, shouldn’t they use their parent’s rigid body? I don’t understand why they just bounce around at random.

aww I see. Sorry, that usually works on other components. Lame. You could try

Destroy(puckObj[0].gameObject.GetComponent());

then when you need it again do

gameObject.AddComponent(Rigidbody)

something along those lines of syntax

That’s one thing I tried (see earlier post), and it causes the objects to behave crazily.

No - colliders and rigidbodies need to be on the same object. There is no hierarchical awareness.

I tried making a copy of the two single objects and RB parent (with no RBs on the children), and then I just swap out the copy for the original when they get stuck. This works!

Almost. The two stuck objects move, rotate, and collide correctly. The only problem remaining is that they no longer trigger a collision event when they hit something. They collide and bounce off other objects, but OnCollisionEnter is not called, which means it doesn’t play the collision sound and other things that need to be done when the stuck objects hit something.

OnTriggerEnter requires that one of the objects has a rigidbody attached to it.

Can you use OnCollisionEnter instead?

I don’t think that’s necessarily true. See the section on Compound Colliders:

http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

You can have multiple child-object colliders, but only one RB.

Typo (now corrected). I AM using OnCollisionEnter, and it’s not being triggered (is what I meant to type).

Ah yes, good point. But notice that the child colliders do not have rigidbodies and the rigidbody does not have a collider.

You get collisions but OnCollisionEnter doesn’t fire? Odd.

Which is how I now have things set up.

And yes, seemed odd to me too.

I got rid of the collision scripts on each of the two colliders in the stuck object, and attached it to the parent, and that did the trick. Looks like the collision event only gets sent to the parent, not the child colliders, even though the parent has no collider of its own.