- Home /
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 :)
Answer by leech54 · May 10, 2017 at 01:24 PM
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.
Ahhhh, ok; i will try that as soon as i got a little bit more time for this. But wouldn't that mean that the child objects would be subject to clipping issues? Sorry for my late reply. I'm a little bit busy right now.
Answer by Twinklier · Aug 20, 2019 at 12:34 AM
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 :)