Connect GameObjects together at runtime?

Hello, I am creating a map with the use of 3D blocks on runtime. I’ve done this before but in a diffent way because the script I made before is for 2D objects and with fix size for each blocks.

Now my question is, how can I connect/snap 2 objects on runtime using a pivot point or a joint? Thanks for the help.

you can join them through scripting

there are three types of joints hinge,spring,character
they are a type of component.
you can add a joint to an object through add component.

example c# code to add a joint to object a and “join” object a to b

Gameobject object_A;
Gameobject object_B;

object_A.addComponent<HingeJoint>();

object_A.HingeJoint.ConnectedBody = object_B;

best of luck! mark if answered

At the moment i work on a track editor for a racing game and i also have trackparts with multiple connectors as child objects. I place them like that:

// C#
Transform Part; // TrackPart
Transform Con;  // Connector object is a child of "Part"

void AttachTo(Transform aOtherConnector)
{
    Transform oldParent = Part.parent; // save the original parent
    Con.parent = null;                 // unparent the connector
    Part.parent = Con;                 // attach our actual part as child to the connector
    
    Con.position = aOtherConnector.position; // place the connector
    Con.forward = -aOtherConnector.forward;
    
    Part.parent = oldParent;           // revert the parenting
    Con.parent = Part;
}

I use “-forward” since the two connectors that should be “connected” face in the opposite direction.

It works pretty well, but i experienced a slight error which grows as you build your TrackPart-chain. I recommend you also use a grid base placement and determine the grid cell the object should be in after you’ve done what i wrote above and set the position / rotation again to absolute values.

This is of course only a solution if you want to place the objects as static objects (like my racing track for example).