- Home /
make a train follow tracks using physics
hello,
I am currently developing an Train Simulator and already got a train with tracks and such. The train uses wheelcolliders for the wheels. Now the problem is that the train drives of the track after while or drives directly off the track on non-straight parts. Now i understand how a real train wheel works but i have trouble recreating that. So my question is how do i make the train wheels stay on the track? I think i need to add a larger piece/wheel to the wheel in the inside of the tracks but if that's the case can someone then tell me how to do that? I do want to use physics so path following is no option.
i hope someone can tell me.
Thanks
I haven't done it myself, but i'd suspect you need to have a pair of wheel colliders, one for the outer part of the wheel that sits on the track, and one for the inner part that sits within it. They should be parent/child relationship, so the two colliders are treated as one. Which is parent I'm not sure about, you will have to play around with it to see if it matters.
tried that but when i start driving the inner wheel will just teleport over the rail and it crashes so i dont think this will work either or i am doing it wrong
Hmm, sounds like something is set up wrong. When i get some time later today, I'll play around with a test and see if i can get it working.
An alternative that just came to $$anonymous$$d is to use raycasting from one wheel to the opposite track. If it's getting too close or too far, AddForce or $$anonymous$$oveToward to get it back in line. Not quite physics, but probably far more reliable and performance efficient than trying to use colliders, and would still retain realism.
gamecreatorTim
Hello, wanted to know, if you found a solution to stabilize the wheels?
Yes I did, you need to use wheel colliders and use sphere colliders between the rails to keep it on the track. But I ended up using splines to make the train follow the track as that is less buggy.
Answer by mmciver · Feb 27, 2017 at 07:37 PM
(from comments)
My mind kept asking myself questions, so I went and played with it.
What you need is a mesh collider. The entire 2-part wheel as a single mesh. You can optionally have a wheel collider on it as well to handle the wheel physics, but the wheel collider is essentially only 2 dimensional, it will only deal with collisions from outside in fore the 'tread of the tire', and will not react to collisions on the face. One challenge with a mesh collider is that you need a high number of faces to the wheel if you don't already. The default Cylinder that I played with has too few faces, and each face on the edge was making it bump along and adds a lot of simulated friction of the wrong type.
But this worked. Here is the test scene i mocked up real quick: https://www.dropbox.com/s/9nf6he4nzizeiui/trainwheeltest.unitypackage?dl=0
I inverted the goal by having cubes as tracks and a few scaled cylinders as the parts of the wheels, and the style is backwards with the large part of the wheel outside the 'track' rather than inside. This is all for the sake of expediency and not having to model the various components first. I tilted the track so I could use gravity rather than having to apply power to the wheels. And I just rotated the cubes by 1 degree each to simulate a curve.
If I was going to turn this into a full solution, I'd have smooth curves, I'd properly weight the wheels and others to prevent them from 'jumping' from hitting the transitions too hard, etc... but i think this shows that it will work for you.
ah, thank you very much!!! i will try to implent it in my train tomorrow and post back the result.
okay, i just implented it in my train but now i need to get it to move. do you know a script that can rotate the wheels so my train can move and i can test? i got 4 wheels on the train btw. And i also need to hingejoint them so they are rotatable
okay, got it to move by using hinge joint but the wheels are very unstable. do you know a better way of moving them?
oh and when the wheels are rotated slowly it is riding fine and i now can drive on non-straight tracks too! thanks you for the example you gave me!
Your answer
Follow this Question
Related Questions
Advanced collisions: mesh collision? 0 Answers
How to avoid Collisions with seams 1 Answer
How to make a train stay on the rails? 1 Answer
Check if player is on track 1 Answer
Edit WheelCollider's Axis, not the Mesh 0 Answers