How to glue Objects together on contact?

Hello everyone,

i am trying to get my feet wet with Unity and what i want to do is sticking an object to another one on contact, so if one of the objects which are glued together moves, the other moves relatively.

I read some threads and they come up with rather simliar scripts so i tried it this way:

void OnCollisionEnter(Collision other)
        if (other.gameObject.tag == "Glueable")
                other.transform.parent = gameObject.transform;

I understand that the effect should be making the hit object a child to the hitting object so they move together.
I tested this but the hit object just makes some little funny moves (not relative to the theoretical new parent) before it falls through the floor -.-

Why does that happen? It seems there are aspects that i don’t understand. Is it the right method to achieve what i want in the first place.

Any help is appreciated :slight_smile:

I am guessing that you have to turn physics of the child off once it sticks to the other or it will keep trying to push the colliders away from each other.

Or u could use a spring joint. It has this springy effect (maybe put a lot of spring value in it) to get the “sticky” feel, like stretching it out and it will snap back like an actual glue-y, slimy thing. You can even put some other value besides Infinity in the break force to simulate the real glue! (You may need to do some scripting or modeling to get the glue-slime effect tho). And just simply use OnCollionEnter to assign the “other” collision to the connected body and voila! I hope this helps :slight_smile: