How do I form a fixed joint from code?

I have two GameObjects, a robot and a mining tool. I would like to make it so that the robot seems to be holding the mining tool, with a FixedJoint, but I don’t know how to do that from code. Thanks!

You need to reference the mining tool as a game object in order to use AddComponent. After you call that method you’ll have to make sure the robot’s holding arm is also a rigidbody, then just feed it as the connected body of the fixed joint. I suggest you experiment within the editor first in order to ensure it moves properly.


joint = miningTool.gameObject.AddComponent<FixedJoint>();
joint.connectedBody = robotArm.rigidbody;

That said, won’t an easier and more consistent way be to just set the robot’s arm as the mining tool’s parent?
I suppose it depends on your setup but you might want to consider that as well.