*UPDATE* : I would advise not coupling the carts the way I did in this video - rigidbodies and collisions happen to be very unreliable joints. Instead, use actual joints! I made a new video specifically going over this: ruclips.net/video/F2CWDjjyadM/видео.html
Hey I came across that same reddit thread before I found this video. Thank you for documenting how this works with example code! I plan to combine this with a hinge joint that has break force, for fun reasons
Glad to hear it has been a bit helpful! I haven't tried hinges yet but when I revisit this I think it will be the next thing I do. The way I set up the collider links on this project are a bit unreliable haha. Let me know if you get them working! I'd like to hear how it goes.
awesome work! Thanks for the walk through of your approach. I’ve done a number of spline based projects and often run into problems converting the time 0…1 into a velocity that is constant on different length splines (calculating the length is mostly an iterative approximation). I’m going to dig through your example and that reddit thread to see if there’s a better way.
Hopefully it's helpful! Sounds like you're animating the transform based on a lerp between the start and the end of the spline? If that's true, my example is pretty much throwing that whole method out of the window lol. I just find the nearest point on the spline, snap the transform to it and make it face the direction of the tangent of the current point, then reapply the velocity onto the rigid body from the last frame. Very "hacky" solution but it allows for the application of physical forces while still following the spline. If I'm understanding your problem properly, you could implement my solution here, but just set the velocity to a constant value - this will stay the same no matter the length of the spline.
Great tutorial. I'm having a problem where the train follows the track in the centre of the room and not where the spline is. Have a feeling it's due to the global vs local position but not sure how to get it to snap to the track!
Seems a bit expensive if you plan on having lots of trains, since they will all have to find the closest point on the spline each tick. Hybrid solution seems the best way if your eally dont feel like writing some code for drag and acceleration
GetNearestPoint returning a local space vector instead of global really screwed me up too (my transform was at 0.9, 0 ,0 so it was just slightly off), glad I'm not the only one!
Hi, great tutorial, perfect for what I'm trying to do. I don't understand how you have attached the train cars, using a joint or in script? It was not shown in the video. Also, what can I add to the script to make the spline work if it is not globally centered. Thank you
It was probably easy to miss but I did cover it in the video - I essentially added collider hooks on the front and back of each cart, then interlinked them such that they'd act like a chain pulling each other along. This proved to be very bug ridden and slow though and I have since re-implemented it using configurable joints instead. I plan to make a follow up video for that next, so if you're having trouble figuring it out yourself stay tuned for that :) For the spline global position problem, I have not spent the time working on that so I couldn't explicitly help you there, but I would think adding the current global position of the spline to each point you're accessing as an offset would be a good place to start. Hopefully that helps and good luck!
Unity supports the spline package so they have a manual and API docs on their website docs.unity3d.com/Packages/com.unity.splines@2.0/manual/ It's definitely helpful, but for me I found it much more useful to just open up the example projects that come with the package and step through the code to see exactly how they use the API. I was able to more easily envision what I needed to do with a bigger picture understanding of how the code works together.
Would anyone have any ideas on how to create splits in the track that the train can choose between. I know you can can create "branches" with the spline tool but how would the play choose between which path to take? Thanks for your help!
I'm using the standard Unity supported spline package: docs.unity3d.com/Packages/com.unity.splines@1.0/manual/index.html You can install it from the Unity package manager
*UPDATE* : I would advise not coupling the carts the way I did in this video - rigidbodies and collisions happen to be very unreliable joints. Instead, use actual joints! I made a new video specifically going over this: ruclips.net/video/F2CWDjjyadM/видео.html
Hey I came across that same reddit thread before I found this video. Thank you for documenting how this works with example code! I plan to combine this with a hinge joint that has break force, for fun reasons
Glad to hear it has been a bit helpful!
I haven't tried hinges yet but when I revisit this I think it will be the next thing I do. The way I set up the collider links on this project are a bit unreliable haha. Let me know if you get them working! I'd like to hear how it goes.
Great stuff! Saved me from a headache I have been having while using the SplineAnimator...
awesome work! Thanks for the walk through of your approach. I’ve done a number of spline based projects and often run into problems converting the time 0…1 into a velocity that is constant on different length splines (calculating the length is mostly an iterative approximation). I’m going to dig through your example and that reddit thread to see if there’s a better way.
Hopefully it's helpful! Sounds like you're animating the transform based on a lerp between the start and the end of the spline? If that's true, my example is pretty much throwing that whole method out of the window lol. I just find the nearest point on the spline, snap the transform to it and make it face the direction of the tangent of the current point, then reapply the velocity onto the rigid body from the last frame.
Very "hacky" solution but it allows for the application of physical forces while still following the spline. If I'm understanding your problem properly, you could implement my solution here, but just set the velocity to a constant value - this will stay the same no matter the length of the spline.
@@paridotgames superb, thanks for your suggestions. Your demo looks super-cool!
we can't see the entire code in the video, please share the code.
Great tutorial. I'm having a problem where the train follows the track in the centre of the room and not where the spline is. Have a feeling it's due to the global vs local position but not sure how to get it to snap to the track!
How did you aligh your bodies to the spline? cause mine always work on the spline with an offset
im having the same issue
@@mr.wolfmaster2892 i got the issue resolved
@@mr.wolfmaster2892 Use localtoworldmatrix
@@boywithbrain7556 how so?
@@mr.wolfmaster2892 use local to world matrix
Seems a bit expensive if you plan on having lots of trains, since they will all have to find the closest point on the spline each tick. Hybrid solution seems the best way if your eally dont feel like writing some code for drag and acceleration
Why is the cart offset from the spline path?
That's awesome
nice tuuut tuuut, nice "tutorial" great explanation
Thank you! 😃
GetNearestPoint returning a local space vector instead of global really screwed me up too (my transform was at 0.9, 0 ,0 so it was just slightly off), glad I'm not the only one!
That was quite the frustrating realization lol
i wanna carry the gameobject conntected to spline container but the locals will not :(
Hi, great tutorial, perfect for what I'm trying to do. I don't understand how you have attached the train cars, using a joint or in script? It was not shown in the video. Also, what can I add to the script to make the spline work if it is not globally centered. Thank you
It was probably easy to miss but I did cover it in the video - I essentially added collider hooks on the front and back of each cart, then interlinked them such that they'd act like a chain pulling each other along. This proved to be very bug ridden and slow though and I have since re-implemented it using configurable joints instead. I plan to make a follow up video for that next, so if you're having trouble figuring it out yourself stay tuned for that :)
For the spline global position problem, I have not spent the time working on that so I couldn't explicitly help you there, but I would think adding the current global position of the spline to each point you're accessing as an offset would be a good place to start.
Hopefully that helps and good luck!
@@paridotgames Thank you!
do you know where to find good documentation on the spline tool? i cant find anything good
Unity supports the spline package so they have a manual and API docs on their website docs.unity3d.com/Packages/com.unity.splines@2.0/manual/
It's definitely helpful, but for me I found it much more useful to just open up the example projects that come with the package and step through the code to see exactly how they use the API. I was able to more easily envision what I needed to do with a bigger picture understanding of how the code works together.
@@paridotgames thank you, subbed
Would anyone have any ideas on how to create splits in the track that the train can choose between. I know you can can create "branches" with the spline tool but how would the play choose between which path to take? Thanks for your help!
ruclips.net/video/ipKeYqYB4oY/видео.htmlsi=Lm2nZLCvvE3gYHW6
What Spline Addon do you use?
I'm using the standard Unity supported spline package: docs.unity3d.com/Packages/com.unity.splines@1.0/manual/index.html
You can install it from the Unity package manager
@@paridotgames thx
Wtf very big
Make bogies rotate instead of whole locomotive
Matrix4x4 localToWorldMatrix = track.transform.localToWorldMatrix;
Matrix4x4 worldToLocalMatrix = track.transform.worldToLocalMatrix;
float distance = SplineUtility.GetNearestPoint(track.Splines[indexSpline], worldToLocalMatrix.MultiplyPoint(transform.position), out Unity.Mathematics.float3 nearest, out float t, 2);
nextWagon.transform.position = localToWorldMatrix.MultiplyPoint(nearest);
Is it for global? 🤔
thats fine worked :D
Thank you very much!