Adding fixedJoint 2D at run time

I’m looking to add a fixed joint to an object when it collides with an object.

the idea is a ball touching this object and a fixed joint attaching it to the object.
I chose to do it this way so that physics handles the ball and I don’t have to set it as a the child of the object it came in to contact with.

Could someone help me understand how to instanciate fixedJoint2D and connect it to an unrelated object through script?


You can add a FixedJoint2D component to an object and make it attach to a Rigidbody2D like this:

FixedJoint2D fj = gameObject.AddComponent<FixedJoint2D >() as FixedJoint2D;
fj.connectedBody = rigidbody2DOfOtherObject;

You can add that in OnCollisionEnter2D() for example to attach it on impact.


This topic was automatically closed after 166 days. New replies are no longer allowed.