Train Spline controller?
Hi,
I am making a sort of train simulator but am having trouble perfecting a multi-car train spline controller. I am interested in replicating what I see in this video. Can anyone glean how they might be approaching this controller?
The track system I have in place is modular and prefab-based, unlike in the video, as random path generation is something i'd like to implement in the future. Each of my tracks has it's own spline aligned to the model and the meta-spline path is calculated by a pathfinding algorithm on start() with each track ordered and added to a list to be referenced by trains as they move along the path.
I currently handle train movement along the path via an iTween implementation where each track has a normalized beginning and end range(0-1) called progress.
progress += speed / currentTrack.length;
I assign a train's progress via copying the train in front of it's progress and subtract an ideal train distance that has been arc-length parameterized for consistency.
float GetNormalizedProgress()
{
//progress = train in front of me's progress(-)the normalized train-to-train distance
return GetTrainByIndex(myTrainIndex - 1).progress -
train_to_train_Distance/train.currentTrack.length;
}
When a train reaches 0 or 1 progress, the current spline index is set to the previous or next spline in the list.
//returns the correct next track
int GetNextTrack()
{
if(progress > 0 && progress < 1) return train.currentTrack;
if(progress > 1)
{
progress -= 1;
//if there is a track after this in the array...
if(train.currentTrack == 0)
{
return tm.orderedTracks.Count - 1;
}
//else, keep going forward
else
{
return train.currentTrack - 1;
}
}
else if(progress < 0)
{
progress += 1;
//track has reached the end, set to beginning
if(train.currentTrack == tm.orderedTracks.Count - 1)
{
return 0;
}
else
{
return train.currentTrack + 1;
}
}
return train.currentTrack;
}
This implementation has worked OK under some circumstances but often produces very glitchy results for heretofore unknown reasons. If anyone has some insight on how, in the video or elsewhere, the train controller could work, or how I might approach it within my described modular system, I would appreciate it greatly.
Thanks!
you need to collect more data about your glitchiness. when does it happen, what values are used, what values should be used?
what I can say is that you could make the code less error prone using $$anonymous$$athf.Repeat. it wraps values around back and forth
Hello ! Hope you are well. It's been quite a time but did you manage to solve the issue for you? I have been trying to do something similar. For a one cart everything is great but managing multiple carts is something I can't get into work. Please help if you could. I know there are tons of assets on asset store that might help but I just don't wanna use them.