Your basic idea is correct, your code may be slightly modified to support this.
Here's the trick: instead of tying your object to the object it collided with, you create a fictitious game object, call it “glue”, at the point of collision and snap your object to the glue. The glue object is then the parent of the object we encountered.
Since the glue is just a dummy object with only components converted and some script, there are no problems with the parent position.
In addition, note that it doesn’t really matter at which point of contact we create the adhesive if we have several points of contact, and this is also easy to extend to supporting rotations, see below.
Thus, in a collision, the only thing we do now is create glue. Here is the code:
void CreateGlue(Vector3 position, GameObject other) {
And this is what the Glue.cs script looks like, it will handle LateUpdate and modify the conversion.
public class Glue : MonoBehaviour { protected Transform stuckTo = null; protected Vector3 offset = Vector3.zero; public void AttachObject(GameObject other) {
Also, note that simply parent objects, as suggested here, can lead to some additional problems, since scaling the parent also scales the children, so you have to re-scale the child to its original size. The problem is that these scaling operations relate to different anchor points, so you also have to make additional adjustments to the position of the objects. Could be done though.
I also created a small sample project, see here (Unity v5.2.f3): https://www.dropbox.com/s/whr85cmdp1tv7tv/GlueObjects.zip?dl=0
PS I see that you mix transforming and rigid semantics, since this is done on kinematic rigid devices, it doesn’t matter, but just a suggestion: think whether you really need to have rigid bodies on objects that are already “stuck” with others, if not - perhaps just remove or disconnect the rigid body instead of making it kinematic.