I need to make a rigidbody move freely along a spline in 2d space, so that it participates in physical interactions with other objects. Something like a SliderJoint2D with limits - but it gives the possibility to move only along the segment between two points.
I was thinking about breaking the whole spline into segments, keep track of which segment the object is on at the moment, and change the SliderJoint2D parameters (angle and limits) dynamically to move the object from one segment to another when moving along the spline.
But I’m not sure how smoothly this transition from one segment to another can be realized, and whether the speed of the object will not be lost at this moment. And in general this approach looks rather ugly.
Maybe there is some simpler way to limit the movement of the object along the spline in Unity? Or maybe it is possible to write custom joints?
Use TargetJoint2D, it’s for when you want to continually set a target point. It can be used for this and is often used for dragging things around using physics.
Here’s an old video of the target following points on a Catmull-Rom Spline:
Here’s it used for dragging:
You can configure the joint stiff etc.
Thanks for the answer, but that’s not it.
Imagine a spline rope that sags in the middle. At opposite ends of the rope are beads that can only move along the rope (the rope itself is static). Under the force of gravity both beads start to move down the rope until they collide in the middle, bounce a little from each other, but then under the gravity they collide again and remain hanging in the middle, pressing against each other.
I want to simulate something like that.

Oh you mean a constrain to spline (or line segment).
So there’s no built-in joint constraint for that so yes, you’ll need to calculate that yourself.
Constraining a Rigidbody2D using your own point-to-point constraint isn’t difficult i.e. only making adjustments to the body position to ensure it’s on the spline. The first hurdle is knowing where, relative to the body position, is the nearest spline position. Knowing that you have a point to move to. It’d be far easier then to split-up your spline into sequential line segments at the resolution that suits your visuals so then it becomes a simple task of finding the closest segment and point on that segment.
If you want a hard constraint then the trick with that move is to ensure you teleport the body to that position i.e. Rigidbody2D.position in FixedUpdate then, when the simulation runs, its velocity isn’t affected so gravity, user-forces, contacts are not affected and it’ll move. Because the simulation can break your constraint, you’re actually better off doing the constraint after the simulation has run so, you can manually run the simulation in fixed-update then constrain. This’ll mean gravity (etc) can break your constraint but you constrain it afterto ensure it’s never visually out-of-constraint.
Of course you can do the much more complex point-constraint using impulses/forces etc but that’s much more complex and beyond what I’d want to go over here. A good reference for that technique might be: https://box2d.org/files/ErinCatto_ModelingAndSolvingConstraints_GDC2009.pdf
Hope that helps.