Attachments and Constraints
Attachments and Constraints
Roblox’s physics system includes two fundamental types of components: attachments and constraints. At a general overview:
Attachmentis where another object connects to a part.
Constraintconnects two attachments. Constraints include hinges, springs, motors, ropes, and other elements which can be used to build mechanical constructions.
To view constraints and attachments, toggle on Constraint Details in the Model tab.
This will turn on the full visualization system, complete with details and hover-over info boxes.
Attachments can be created using either Studio tools or script commands.
A basic attachment can be created from the Model tab by accessing the Create button’s drop-down menu and selecting Attachment.
Once the tool is active, drag your mouse over the surface of a part. When the attachment is in the desired position, click to create it.
To create an attachment in a script, simply use
Instance.new("Attachment") and then parent the attachment to the target part:
-- Create attachment on red block local attachmentA = Instance.new("Attachment") attachmentA.Parent = game.Workspace.RedBlock
BasePart— they cannot simply be orphaned inside the workspace or any other object..
Attachments can be positioned anywhere relative to a parent
BasePart. Note that attachments are not restricted to the surface of a part — they can be positioned inside the part or even outside its bounds.
To reposition an attachment in Studio, select it and move it with the Move tool:
Within a script, an attachment’s position can be changed by its
Attachment/Position|Position property. For example, the following code creates an attachment, moves it 1.5 studs along the Z axis, and then parents it to the gray block:
-- Create and reposition attachment on gray block local attachmentB = Instance.new("Attachment") attachmentB.Position = Vector3.new(0, 0, -1.5) attachmentB.Parent = game.Workspace.GrayBlock
Constraints are used to connect two attachments. For example, the
RodConstraint ensures that its two attachments stay a fixed distance away from each other. If one attachment moves, the other will be pushed or pulled to follow.
As with attachments, constraints can be created using either Studio tools or script commands.
To create a constraint in Studio:
- In the Constraints section, select the desired constraint type from the Create drop-down menu.
- On one part, click the location where you’d like to create the first attachment.
- On the other part, click where you’d like to place the second attachment. This will complete the connection with the chosen constraint type.
To create a constraint via a script, begin by creating both attachments on the appropriate parts. Then, using
Instance.new(), create the constraint and assign its
Attachment1 properties to the two new attachments.
-- Create attachment on red block local attachmentA = Instance.new("Attachment") attachmentA.Position = Vector3.new(0, 0, -0.5) attachmentA.Parent = game.Workspace.RedBlock -- Create attachment on gray block local attachmentB = Instance.new("Attachment") attachmentB.Position = Vector3.new(0, 0.5, 0) attachmentB.Parent = game.Workspace.GrayBlock -- Create a rod constraint between the two attachments local rodConstraint = Instance.new("RodConstraint") rodConstraint.Attachment0 = attachmentA rodConstraint.Attachment1 = attachmentB rodConstraint.Parent = game.Workspace.RedBlock rodConstraint.Length = rodConstraint.CurrentDistance
Constraints can be toggled on and off using their
Constraint/Enabled|Enabled property. When disabled, a constraint will not apply any kind of force or torque on its attachments.
The script addition highlighted below toggles the constraint’s
Enabled property on and off every 2 seconds.
-- Create a rod constraint between the two attachments local rodConstraint = Instance.new("RodConstraint") rodConstraint.Attachment0 = attachmentA rodConstraint.Attachment1 = attachmentB rodConstraint.Parent = game.Workspace.RedBlock rodConstraint.Length = rodConstraint.CurrentDistance while wait(2) do rodConstraint.Enabled = not rodConstraint.Enabled end
Attachments have an orientation as well as a position. Some constraint types require that their attachments are rotated correctly, otherwise the mechanism will not work as intended.
For example, a
HingeConstraint forces two attachments to snap together when the game is run and lets them rotate. To make a hinge behave correctly, it’s important that you orient each attachment so that its Axis property — visualized below by the yellow arrows — point in the same direction along the desired axis.
As you can see in the following video, this hinge rotates correctly around the X axis because the Axis of both
Attachment1 are aligned.
In contrast, one attachment on the following hinge constraint is rotated 45° along the Z axis:
When playtested, the two parts are pulled together and can only rotate along the axis of the attachments.
Now that you understand the basics of attachments and constraints, please explore the articles below on how to build common mechanisms.