2
\$\begingroup\$

How can I make a GameObject attach (or "stick") to another GameObject after collision? The problem: I want the GameObject to attach after collision even if it changes scale.

What I have tried:

protected Transform stuckTo = null; protected Vector3 offset = Vector3.zero; public void LateUpdate() { if (stuckTo != null) transform.position = stuckTo.position - offset; } void OnCollisionEnter(Collision col) { rb = GetComponent<Rigidbody>(); rb.isKinematic = true; if(stuckTo == null || stuckTo != col.gameObject.transform) offset = col.gameObject.transform.position - transform.position; stuckTo = col.gameObject.transform; } 

This code makes a GameObject attach perfectly after collision. But when that GameObject changes scale (while it's attached), it visually no longer looks attached to whatever it collided with. Basically, this code makes the GameObject stick with only the original scale at the moment of the collision. How can I make the GameObject always stick to whatever it collided with? And with whatever scale it has during the process?

\$\endgroup\$
1
  • \$\begingroup\$ This question is not a duplicate. The OP's code behaves the way the code in the answer of the supposed duplicate does. This question asks how to achieve the same effect but continue to stick when the object is scaled. \$\endgroup\$ Commented Nov 17, 2015 at 14:30

1 Answer 1

2
\$\begingroup\$

It's much simpler than you think:

void OnCollisionEnter(Collision col) { transform.parent = col.gameObject.transform; } 

You don't need the late update.

\$\endgroup\$