object following a path, like train - railroad

Hi, what techniques can I use to make object follow a path? something like a train following railroad.

it must be able to follow strait lines, and curves as well.

speed by which gameobject will move will not be constant, it might accelerate or slow down.

I'm new to unity and don't know how to approach this.

only solutions I could think of were 1: animating path in 3d program, but then how do set speed in realtime? 2: adding kind of pinpoint objects, my car or whatever will move towards. but will it be possible to get exact form of path object this way? I could add pinpoints at the same locations where vertexes are on path object, basically a turning points of curve. but is there any function that can track in realtime which pinpoint is next and function that can calculate curve between last and next pinpoint?

I would suggest solution 1 because when you want a train follow a railroad track you need the exact position and rotation of the individual objects.

Reference for Animation Curves

For changing the velocity there might be 3 solutions:

  1. You could also use an animation curve.

  2. Set Animation Events for specific frames. (also useful for signals or speed limits at stations)

  3. Calculate the velocity depending on the pitch and roll of your game object.

Waypoint/Bezier examples can be found in the Unity forum.


was the best easy solution I found, but speed and rotation proved tricky

You want to look into:

  • Pathfinding
  • Steering
  • Waypoints

Those topics will cover what you want to accomplish. Unity doesn't give a solution for any of these out of the box, but there are several available online. Read up on the articles for the tags I added to your question.

I also came across curvy from fluffyunderware or something. I didn’t try it yet but seems promising. iTween also seems to deliver something in regards of path finding, but the demos that i saw wasn’t that “curve-y”.
I’ll try a bit more and maybe update this answer.