I have this sliding door where i want to use a joint to limit its motion:

Screenshot of a sliding door, partially open

...but I am confused about how to configure a joint to provide this sliding motion.

I've found the Hinge Joint component, but the comments below say this is for rotating motion. What kind of joint do I use to create a sliding motion?

  • \$\begingroup\$ A hinge joint is to rotate an object around a anchor point. With a sliding door, I fail to see how a hinge joint would help. Why even use a joint at all? All you need to do is translate the sliding portion of the door by its Vector3.left \$\endgroup\$
    – jgallant
    Sep 9 '16 at 10:56
  • \$\begingroup\$ I am using HTC vive where i have controller and joint is suitable to do this task with controllers \$\endgroup\$ Sep 9 '16 at 11:44
  • 4
    \$\begingroup\$ It would be more suitable to write a sliding door script, and manage the movement without a hinge joint. Unless you are rotating the door, a hinge joint makes no sense here. \$\endgroup\$
    – jgallant
    Sep 9 '16 at 12:03
  • 1
    \$\begingroup\$ Folks, no need to get hung up on the "hinge" word. It's clear from the context of the sliding door that they want a sliding physics constraint, which is a perfectly reasonable situation in which to use a joint. It's just that Unity doesn't make it obvious how to create such a joint. I'll elaborate in an answer. \$\endgroup\$
    – DMGregory
    Sep 17 '16 at 17:08

As pointed out in the comments above, the Hinge Joint is not the right type of constraint to use for a sliding door. It handles rotational movement, like a door that swings.

For a sliding door, we'd instead use what's called a "Prismatic Joint"

Unfortunately, Unity doesn't have a built-in 3D Prismatic Joint component the way it does for springs and hinges. But we can create one using the Configurable Joint.

True to its name, this has a pile of configurable parameters, but we only need a few for this situation...

  • Connected Body: you can leave this as "None" if you want your door to slide with respect to a fixed track in the world. Optionally, you could create a track object and connect the door to that.

  • Anchor: this is the position in your door's local coordinates that should act as the attachment point - like where it hooks into your track. It looks here like your pivot is in the bottom-left corner of the door, and that's a sensible place, so let's use (0, 0, 0) here.

  • Axis: this is the direction of movement, in your object's local coordinate space. In my tests, my door slid along its local X axis (1, 0, 0). If you're using Z, interchange X&Z below.

  • Auto-Configure Connected Anchor: uncheck this. If checked, the joint will treat the door's placed position as the center of its movement range, allowing it to slide both left & right. I expect though you want to place your door in either the open or closed position, so we'll want to manually control this.

  • Connected Anchor: this is the position of the center of your track. If you specified a Connected Body, then it's in that body's local coordinates. Otherwise, it's in worldspace. To find the right value to use, slide your door until it's half-open, copy the worldspace position of its pivot, and use this value.

  • X/Y/Z Motion: X should be set to "Limited", and the others should be set to "Locked." This gets us the sliding motion we want.

  • Linear Limit: This is how far the door should be able to slide from the center point of its track we defined earlier. It's measured in worldspace, and should probably be about half the width of your door, or a little less.

  • Finally, select the Rigidbody component on your door and activate all three rotation constraints, to keep the door from twisting. The joint handles the position constraints just fine, so you don't need to fiddle with those.

That should do it. You can also play with the drive settings if you want the door to close itself, or adjust its Rigidbody's drag to help it slide to a stop.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.