Coding a train?

Hi,

Does anyone have some basic scripts they might be willing to share for making a train move? I have assets for the train, a few passenger cars, and the tracks. I’m looking to push a button to move forward, and another to move backward. I hear that turning is the hardest part. I looked into pathfinding/waypoints for turning but at this point I’m still learning unity and coding so I should slow down and start with forward and backward motion first.

Please help, thanks!

1 Like

Hey,

fellow beginner here, what you could do if the train has a simple path is use a collider on an invisible quad and when the train hits it, it could turn 10 or 20 degrees or so.

Hope this helps get the brain going :slight_smile:

I would say that any script that moves anything around would work. Modifying the transform is all you need to do.

1 Like

I saw someone recommend getting the train & carriages to follow a spline that was the rail track.

1 Like

There is a Spline system on the asset store that has a component in it just for moving multiple objects along paths and the example that can be seen in the video below is a toy train with multiple carriages all with independent wheels moving along a track that is also generated by the asset.

This can be of use to you, it’s a bit complicated but I think it’s a good read.

1 Like

That cost about $90-150