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:
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 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.
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.
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.
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.