I have this sliding door where i want to use a joint to limit its motion:
...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?
Answer
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.
No comments:
Post a Comment