Getting a train car to follow a train

I’m trying to implement this operation by having it so when my train collides with a train car, the train will give the car some of it’s data so the car will match the speed of my train and follow it around.

However, to do something like that I need to make it so that the car will constantly get data from the train somehow.

So I guess the TL;DR version of this would be, how do I give a variable from one object script to another object constantly?

Is it a physics object? You could just add a joint to the two objects.

If not, you can just give the other car a reference to it and have it grab the info itself.

Okay, I figured I would need a reference but I didn’t know how to do it, after doing some searching online I figured out how to do so. Thank you for the help

You should consider the train as a single object rather than trying to link car after car after car. After all, they aren’t changing places frequently. :wink:

A train class would have an array of cars (where locomotives are also cars, just powered ones if you need that).
You can then just update each car’s game object transform at once, moving them all by a certain amount forward - given a straight line.

Do you also want the train to ride on curves? That is super-tricky! Now you need to consider each car running on axles of wheels, usually one axle at the front and one at the back, and those need to stay on the track whereas the center of a car can be slightly off-track in curves.

You may also have to consider that wagons in a curve increase distance between each other slightly due to the coupling swaying to one side.

Also, there are assets on the store that let you do this in case you’d rather spend some $ and have it just work with little effort.

So when a train cart is connected, the previous carts in the train should not be the same object. They should be following a path and have a delayed response. If the front of the train turns corner at a specific vector position the cart behind it won’t turn the corner until it reaches that specific vector position.
So all in all you need to make a vehicle that moves along a track and then any of its vehicle subjects, that are attached from behind are only maintaining a constant distance from the primary locomotive in front and repeating the same actions as the locomotor does when hitting the track triggers.

it’s all about the design of the track and how a vehicle behaves can be communicated by the track segment when the relevant carriage passes over.