# Somewhat complex Train

**URL:** <https://discussions.unity.com/t/somewhat-complex-train/422400>\
**Category:** Scripting\
**Tags:** Scripting\
**Created:** [September 6, 2010, 5:43pm UTC](https://discussions.unity.com/t/somewhat-complex-train/422400 "2010-09-06T17:43:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_73493719](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@anon\_73493719](https://discussions.unity.com/u/anon_73493719)\
**Post date:** [September 6, 2010, 5:43pm UTC](https://discussions.unity.com/t/somewhat-complex-train/422400/1 "2010-09-06T17:43:02Z")

</div>

I’ve done a lot of looking around on different ways to get trains to follow turns, and I found several ways that would be great for a standard train system.

However, I have a trolley-like platform that’s able to move back and forth at the control of the player. The player is able to start and stop the train at any time he wishes, and the train is able to move in both directions.

I have the train itself set up so that it can move back and forth, with the player inside, at the control of the player. However, I’ve tried many ways of getting the train to turn or switch tracks, depending on how the controls are set.

I have a script for causing the train to go up a hill, but it seems more complicated than it should be.

I looked at the Spline script on the wiki, and it’s great for causing the train to turn the corner at a nice, smooth rotation. However, it seems to be only good for one direction, and only once. Theoretically, in order to have the train be able to stop and go the other way in mid-turn, there would have to be two Spline curves on the same track, since the train follows the points in alphabetical order. Not to mention the trouble of having the train come to a split in the track and having to be able to choose which line to follow based on player input.

The track system is supposed to be able to have possibly dozens of hills, turns, and splits all throughout, so I need a solution that’ll allow me to effectively prefab something into the track segments.

With this in mind, I tried various trigger systems. But that proved to be inconsistent (messes up more often with slower speeds, for example) and held different results in different builds (Test run in the editor showed the train right on the mark, but building the level and testing it showed the train turning too much).

I even tried raw physics. I didn’t think it would work, and it didn’t; the train simply flew off the tracks the moment it hit any kind of turn or hill at any relatively high speed, no matter the shape of colliers I used.

What would you guys suggest for getting this to work? I’d really like to use the Spline curve system, but it just doesn’t seem like I’d be able to change the curves on the fly like I’d have to be able to. I tried this, but it doesn’t calculate the curve since that script calculates the curves when you connect the object to the line in the editor, not in run time.

---

<div class="post-metadata">

**Author:** ![\_Adriaan\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/_adriaan_1/32/323848_2.png) [@\_Adriaan\_1](https://discussions.unity.com/u/_Adriaan_1)\
**Post date:** [September 6, 2010, 6:02pm UTC](https://discussions.unity.com/t/somewhat-complex-train/422400/2 "2010-09-06T18:02:01Z")

</div>

Although I do not have a direct and tested solution for you, I think I have something you might want to look at.

First of all, you could see the moving train not as a whole but as sections of wheels and on top of that wagons. If you move these sections quite roughly over the track and the wagons are simply the connections between these sections, the wagon already moves much smoother than as a whole.  
Second, the train sections could probably be moved according to the tracks beneath it, right? ‘Simply’ said: if the train runs over a track going straight, each part of the track is telling the sections of the train to go **from** a and **to** b every time the train enters the track section. Corners could even do the same - you could split them into many parts of straight tracks and give it another visual or you could try to make the curve with math.

I hope you get what I mean. Please let me know if you have figured it out!

---

<div class="post-metadata">

**Author:** ![anon\_73493719](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@anon\_73493719](https://discussions.unity.com/u/anon_73493719)\
**Post date:** [September 11, 2010, 6:07pm UTC](https://discussions.unity.com/t/somewhat-complex-train/422400/3 "2010-09-11T18:07:49Z")

</div>

I think I found a way to do this.

I located a plugin script, iTween, and used it to create paths that you can change in run time. So far, it seems to be working pretty well.

---

<div class="post-metadata">

**Author:** ![\_Adriaan\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/_adriaan_1/32/323848_2.png) [@\_Adriaan\_1](https://discussions.unity.com/u/_Adriaan_1)\
**Post date:** [September 11, 2010, 7:20pm UTC](https://discussions.unity.com/t/somewhat-complex-train/422400/4 "2010-09-11T19:20:56Z")

</div>

But isn’t iTween pretty heavy on performance?

---

<div class="post-metadata">

**Author:** ![anon\_30430321](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@anon\_30430321](https://discussions.unity.com/u/anon_30430321)\
**Post date:** [September 11, 2010, 10:27pm UTC](https://discussions.unity.com/t/somewhat-complex-train/422400/5 "2010-09-11T22:27:43Z")

</div>

> [@""](#):
>
> But isn’t iTween pretty heavy on performance?

Nope 😉

---

<div class="post-metadata">

**Author:** ![Exagerate](https://avatars.discourse-cdn.com/v4/letter/e/85e7bf/32.png) [@Exagerate](https://discussions.unity.com/u/Exagerate)\
**Post date:** [March 10, 2013, 10:29am UTC](https://discussions.unity.com/t/somewhat-complex-train/422400/6 "2013-03-10T10:29:00Z")

</div>

Sorry to revive an old thread, i’ve been trying to figure out how to get my train on the move for a while now, I have a model for the front vehicle, which has doors that work with iTween, and a dummy vehicle behind that is essentially being pulled. Is there any documentation available to boost my learning of how to simulate a rail vehicle in iTween? I have succesfully got one working with iTween visual editor, but I got rid of it and stuck with the original iTween out of preference as I can then have a file saved for each movement, and it seems preferable to have seperate scripts without visualisation, which, I do not need anyway.

C# or Javascript tutorials or examples would be awesome, or just something that demonstrates non-visual editor iTween movement along a path, paths defined within a script of my own making.

Kind regards,
