How can I create a joint between objects which only allows for rotation around an axis?

I am trying to design a rather simple “multi-car” train and have decided to create a prefab for each of the cars. My ultimate goal is to be able to hinge one or more of these cars together and have them run along a single-rail track created with Dreamteck Splines.

My prototype for the car is rather simple: I have a capsule for the body of the car and at the bottom of one of the lengths of the capsule, I have a flattened cylinder which is partially recessed into the cylinder and is going to provide the base for my “slot” which will run along the rail. In the provided image, the red flattened cylinder is my wheel base and the yellow capsule is the train car. The train car has a rigid body attached.

What I would like to accomplish is somehow linking the cylinder to train car such that: a) the cylinder is synchronized with the train car as it moves in space, and b) the cylinder can rotate a full 360 degrees in either direction, with a center of rotation that is perpendicular to the center of the cylinder and through the body of the capsule. I have resolved (a) by parenting and positioning the capsule and cylinder components to a “TrainCar” game object, so they can move independently.

I would like to now try to resolve (b) by making sure that the cylinder cannot move relative to the capsule, with the exception of only rotating freely around that axis. I have looked at the different joints and it looks like all of the joint types with the exception of the fixed joint might allow for this type of behavior. The configurable joint (which the documentation notes that all other joints derive from) seems pretty daunting in the number of settings.

That being said, can I use one of the simpler joints to do what I am trying to accomplish? If not and I need to go with the configurable joint, which settings would I need to play with to accomplish my goal?

The reason I am doing this is because I want to have one “main” train car which follows a spline. I had successfully added a hinge joint between multiple cars, however, as the lead car followed the spline, the remaining cars hinged as expected but of course did not follow the smooth curves. I had resorted adding a spline follower to each car, but I then had to apply an offset to all cars so they would be behind one another and when the main train car stopped, they all stacked up in one place.

I would like to have a train I can construct of any arbitrary number of these cars, have only one main train following a spline (and the slots of the cylinders on each car follow a rail generated from the spline), but have all cars hinged and smoothly follow all curves of the track.

Any guidance would be greatly appreciated!

114972-railcar.png

I’m not sure if i got correctly what you are trying to do with respect to your cylinder and capsule but try the following:

  • both capsule and cylinder get a rigidbody.
  • give the capsule a hinge joint.
  • Set the connected body to the cylinders rigidbody.
  • set the joints axis to the axis of the capsule that you want your cylinder to rotate around.
  • set the anchor Position of the joint to the position of your cylinders transform.

with this i think it should produce what you are looking for.

For your problem b) i can just think of getting your offset calculation to work. I can’t really see a solution to this which relys purely on physics.