You will want to test if the player collides with the rail; and if so, snap the player onto the rail.
###2D Illustration
2D Illustration
Here, the blue line represents the rail; the red circle represents the original position of the player (the circle could be any shape); the maroon circle represents the position of the player after being snapped to the rail; and the purple line represents the direction of translation/movement. This line is perpendicular to the rail, so the player is snapped to the closest point possible. As the illustration demonstrates, the player must move to the center of the rail in one direction, while staying centered on the line of translation. This could be calculated using the following pseudocode:
// form equation of line of the rail
float rail_slope = rail_dir.y / rail_dir.x;
// y - y1 = m*(x - x1) => y = m*x + (m*-x1 + y1)
// y = m*x + b
float rail_y_int = rail_slope * -a_point_on_rail.x + a_point_on_rail.y; // y-intercept, or |b|
// form equation of line of translation
float translation_slope = - 1 / rail_slope; // a perpendicular line's slope is the opposite reciprocal
// the player will always be on the line of translation
float translation_y_int = translation_slope * -player_pos.x + player_pos.y;
// now find the intersection between the two lines, which is the new location of the player
// f(x) = m1*x + b1; g(x) = m1*x + b1;
// intersection: m1*x + b1 = m2*x + b2 => m1*x - m2*x = b2 - b1
// => x (m1 - m2) = b2 - b1 => x = (b2 - b1) / (m1 - m2);
// y = m1*x
float snapped_x = (translation_y_int - rail_y_int) / (rail_slope - translation_slope);
float snapped_y = rail_slope * snapped_x;
player_pos = new Vector2(snapped_x, snapped_y);
###3D Illustration
3D Illustration
The player before being snapped to the rail:

The player after being snapped to the rail, with the plane of translation:

As the illustration demonstrates, the player must move to the center of the rail, while staying on the plane of translation. I don't know 3D geometry as of now, so I cannot provide the pseudocode for 3D.
