Does anyone know a spline system with junctions (for a train simulator)?
I've been trying to create my own train sim for a long time now, but always have failed beacuse of not knowing a good way to controll the train and keep it on the track. If anyone knows a free spline tool that alows me to controll a junction trough a C# script or knows a way to pull it of in a other resonable way, please help!
@fabi090806 Hey, I found the discussion I had referred to - I didn't realize that those forums get moved to this forum as answers when they are closed
Hope it helps!:
https://answers.unity.com/questions/1702609/how-can-you-make-a-path-with-a-switch-in-it.html
Answer by streeetwalker · Apr 13, 2020 at 04:50 PM
@fabi090806, Sebastian Lague on youTube has an excellent spline based road creator that will work perfectly. It allows open or close spline paths.
It does not have junctions built in, but it is easy to create junctions by placing two splines touching or near each other, and then creating a Trigger Collider (can be on an empty object) and placing it at the location where you want a junction. You can then detect the trigger and switch splines for your train. You'd have to work out the details
I did this recently as proof of concept for another person asking for help and it worked pretty slick, though in the end they took Sebastian's code (it's a free download) and modified it to effectively (I don't think actually) join splines. You can do a search on the Unity Forum for that discussion (not this forum).
I am already using Sebastian's spline road creator in my testing/prototype project so it is easy for me to impliment this, thank you!
Do you have a link to that discussion or know the keywords to search for? I can't find it on the Unity Forum :/ Because it does not work too well at the moment...
Sorry, I just spend a half hour searching and I cannot find it, and I cannot find my replies to it, which is weird. I know the date was around Feb 27 of 2020 because that is the creation date of my proof of concept project that I mentioned.
I don't think they would have deleted the thread because as I recall he/she posted the code in their last reply explicitly so that others could use it.
I do recall the gist of poster's code: what they had done was calculate a point on one spline that was closest to the end point of a second spline, so that when the object moved and hit that point they could decide whether or not to switch to the second spline. I don't recall the exact code though.
$$anonymous$$y way of doing it was perhaps simpler, but I did need to fiddle with the locations, curves and twists using by setting the spline point rotations to get a smooth move to the second spline. the spline's location and orientation is hard to judge because it just appears as a line, so I had to move the camera around to make to the spines aligned in all 3 dimensions.
I see in your graphic your train looks like it is rotated 90 degress? That probably has to do with your model's initial rotation - you might need to parent it to an empty transform to set the correct orientation. Off the top of my head, doesn't Lague's code align the objects transform.forward along the direction of motion on the spline?
Everytime I moved the trigger my results change and the reason the train is rotated like this is because the joints are going haywire. I have connected the wheels to the train (idk the exact English names) with joints. I have managed to align the trigger exactly on the point where the spline starts, but because there are not just going to be like two junctions but about 50 on every route, I guess I will have to figure out to do it the way you described it...