Roblox's physics system allows you to construct moving mechanisms like doors, rotating platforms, and even vehicles using constraints. For instance, a swinging door can be built using the HingeConstraint.
Start by creating parts for the door and its attachments. Attachments are where one object can connect to another. These attachments will later be used to connect the door to its frame with a hinge.
Create two parts with names like Door and DoorFrame.
Select DoorFrame. In the Properties, enable Anchored so it won't move.
In the Explorer, hover over DoorFrame and add a new Attachment. Repeat the same to add an attachment to the Door.
Rename the attachments to indicate what they're attached to, such as DoorAttachment and FrameAttachment.
Move the Attachments
New attachments are created in the center of a part. So they can work with the door, the two attachments need to be moved to face each other.
To view constraints and attachments, in the Model tab, toggle on Constraint Details.
In the Explorer, select FrameAttachment.
Press F to focus on the attachment and zoom in if needed. Then, use the Move tool to position the attachment on the surface of the door frame, facing the door.
Repeat the same to move DoorAttachment. Your attachments should be positioned on the surface facing their counterpart.
Rotate the Attachments
The orientation of an attachment affects how a constraint can move. For the door, both attachments must be rotated so the hinge swings left and right, like those on a standard door.
On the door frame, hover over FrameAttachment. Notice the yellow arrow. This arrow, the axis, determines the hinge's rotation.
For accurate rotation, turn on snapping in Model → Snap to Grid, and checking Rotate. Set the value to 90.
Use the Rotate tool to orient both yellow attachments to point upwards. If your axes are already vertical, no action is needed.
Adding the Constraint
Remember, constraints are a way of connecting two attachments to move in a specific way. This door will use a HingeConstraint, a common constraint that rotates objects along the axes of two attachments.
Under DoorFrame, create a new HingeConstraint.
In the constraint's properties, find Attachment0. Click the empty box right of the property and then, in the Explorer, click DoorAttachment.
Repeat the same process by connecting Attachment1 to FrameAttachment. The properties should appear as below.
Test the project by walking into the door with your character.
Adjust the Door
The door is currently able to swing past the door frame. This can be fixed by adjusting the hinge limits.
In the properties for HingeConstraint, find and toggle LimitsEnabled. When active, you can set the rotation limits of the hinge.
To make sure this is oriented correctly, select DoorAttachment and use the rotate tool so the orange arrow points towards the door frame, like below.
Under the Limits section of the properties, both LowerAngle and UpperAngle to -90 and 90, respectively. This creates a range of motion like the image on the left.
Test the door and notice that the hinge is now limited.
With the door complete, take some time to explore using hinge constraints in different situations, such as a trap door or a swinging axe trap.