How to make my own Joint?

I want to create a SliderJoint. I know how to create such a thing with a ConfigurableJoint, but that’s not exactly designer friendly.

The options I see are but 2̶ 1.
O̶p̶t̶i̶o̶n̶ ̶1̶:̶ ̶i̶n̶h̶e̶r̶i̶t̶ ̶f̶r̶o̶m̶ ̶J̶o̶i̶n̶t̶ ̶a̶n̶d̶ ̶c̶r̶e̶a̶t̶e̶ ̶m̶y̶ ̶o̶w̶n̶.̶ ̶I̶ ̶h̶a̶v̶e̶ ̶h̶o̶w̶e̶v̶e̶r̶ ̶y̶e̶t̶ ̶b̶e̶e̶n̶ ̶u̶n̶s̶u̶c̶c̶e̶s̶s̶f̶u̶l̶ ̶i̶n̶ ̶t̶h̶i̶s̶ ̶s̶i̶n̶c̶e̶ ̶i̶f̶ ̶I̶ ̶t̶r̶y̶ ̶i̶t̶,̶ ̶U̶n̶i̶t̶y̶ ̶w̶o̶n̶’̶t̶ ̶l̶e̶t̶ ̶m̶e̶ ̶a̶d̶d̶ ̶t̶h̶e̶ ̶c̶o̶m̶p̶o̶n̶e̶n̶t̶ ̶t̶o̶ ̶g̶a̶m̶e̶o̶b̶j̶e̶c̶t̶s̶ ̶(̶p̶r̶o̶b̶a̶b̶l̶y̶ ̶b̶e̶t̶t̶e̶r̶ ̶t̶h̶i̶s̶ ̶w̶a̶y̶ ̶s̶i̶n̶c̶e̶ ̶t̶h̶e̶ ̶i̶m̶p̶l̶e̶m̶e̶n̶t̶a̶t̶i̶o̶n̶ ̶w̶o̶u̶l̶d̶ ̶b̶e̶ ̶h̶e̶l̶l̶)̶.̶ Jump to why.
Option 2: write SliderJoint as an authoring component for a ConfigurableJoint and put HideFlags.HideInInspector on the CJ. This works great, however I can’t drag and drop the SliderJoint in Joint fields like [SerializeField] public Joint myJoint;. Is there a solution to this? Alternatively I can give the ConfigurableJoint HideFlags.NotEditable instead, but that’s not as clean.

Just to clarify, you mean making something like SliderJoint2D but in 3D? eg. A joint that lets you have a sliding door effect.

Yes. Though it is technically called a Prismatic Joint, I opted for the name Slider Joint to keep the naming more consistent across dimensions (I might change this later, not sure yet).

I haven’t really looked into it much, but couldn’t you just lock some of the axis on a RigidBody so it can only move along one of them, and then have a script confine your object to certain bounds?

I would rather use a Joint for all the functionality that comes with it, like connecting it to a different Rigidbody, the ability to break it, and so I can drag and drop it in fields that ask for Joint.

btw I found an unsatisfying way to fix option 2. I can give the ConfigurableJoint the HideFlags.NotEditable instead. Hopefully there is a better solution than this, but if not then I’m going with that.

I haven’t tested this myself, but you might implement a custom joint by constraining the velocity (Rigidbody.velocity) so it adheres to your joint definition.

For example, if your joint makes the rigidbody to move on a given plane only, then you may remove the part of the velocity that is aligned with the plane’s normal. For a slider joint, you would keep the part of the velocity aligned with your slider direction.

Thanks for your advice on how I could do it with a custom solution.

However, after some trial and error and some small tests with both solutions, option 2 just proves to be the stronger and easier solution. Option 1 has me scratching my head fixing small and tedious bugs, option 2 just works because it’s a configurable joint doing all the heavy lifting.

I can even cast it to a Joint and back:

public class SliderJoint : MonoBehaviour
{
    private ConfigurableJoint configurableJoint;

    public static implicit operator Joint(SliderJoint sliderJoint) => sliderJoint.configurableJoint;
    public static explicit operator SliderJoint(Joint joint)
    {
        var configurableJoint = (ConfigurableJoint)joint;
        var sliderJoints = configurableJoint.GetComponents<SliderJoint>();
        var sliderJoint = Array.Find(sliderJoints, sliderJoint => sliderJoint.configurableJoint == configurableJoint);

        return !sliderJoint ? throw new InvalidCastException("Specified cast is not valid.") : sliderJoint;
    }
}

I still can’t drag and drop it, but since for option 1 I still can’t inherit from Joint it would be even more impossible. At least with option 2 I have a Joint to give.

Note btw that I’m doing some background work so that the ConfigurableJoint always exists, from SliderJoints creation until its ultimate demise.