How do I make my minecart stay on the rails?

I really don’t want to use CFrame. This is in a large game and I don’t want to slow the server down making CFrame changes very often. It will also look choppy. (And yeah, of course I want players to be able to ride in it! How cool would that be?)

I like the idea of BodyPosition, but would I not run into the same problem? How do you recommend tweening them? Making it rotate like that seems like it would not be fun to do, or an elegant solution.

Ideally, I’d find some way to get it to stay on tracks using some physics wizardry or something… But if not I guess I’ll try CFrame since it seems to be the easiest way.

4 Likes

Here’s an interesting video on the physics of real-world trains. I don’t know if you can recreate this in roblox, but it might be interesting to look at.

13 Likes

Yeah I’m not very good at anything physical in Roblox…

How do those old roller coaster games do it? I know it has nothing to do w/ CFrame.

2 Likes

Why? Because trains!

Railcar.rbxl (39.5 KB)

13 Likes

Try using a redstone torch.
If that fails, use CFrame. It is practically the universal way to move both vehicles and interactive models on Roblox.

19 Likes

Old roller coasters still work today, the problem is they not very “reliable” in terms of running. They can either run smooth at one point, and the next thing you know it, it launches it self off the track. This has many factors, depending on the speeds on the tracks, the way the “connectors” are designed holding a cart to the track, and the player actions on it. (such as trying to jump up and down on the cart moving, which may cause it to break). If would like anymore information, you can either DM me or contact me on Discord! TheDreamDealer#7445

Note: I used to create and make old coasters :slight_smile:

10 Likes

Alright I’ll just suck it up and use CFrame. It will probably be a lot easier than any other way. Thanks for the help guys.

3 Likes

Okay, I was wrong, CFrame is not my solution, for reasons you stated. I need players to be able to ride in the minecart. How do you tween the BodyPosition to do this?

3 Likes

I’d say make a node path for the train, then just have a bodyposition in the train that you progressively change the position of to the node, once the magnitude of the train and the node are below a certain point (i.e. it’s reached the next node along) change the bodyposition’s position to that of the next node along.

3 Likes

What about turns? That seems like it’d be pretty complicated.

2 Likes

It shouldn’t have a problem actually going round a turn, but to make it face a certain direction you would probably want to use BodyGyro

1 Like

I have a Minecart in my game, and looked at a bunch of approaches while working on my project.

Don’t:

Physically model the Minecart based on real-world mechanisms:
Real-world mechanisms work because of real-world laws of physics. When you model mechanisms based on their real-world counterparts, you run the risk of it breaking down when those laws aren’t the same in Roblox. You also take on all limitations of the real-world mechanism e.g. cart will derail if moving too fast / may derail if mass not micromanaged to reflect real-world counterpart.

Use CFrame:
Moving objects like characters use physics to move around – they’re not built to work with objects moving with CFrame. You lose a lot of awesome functionality like interpolation and bodies following the moving part horizontally, and experience problems like bodies clipping through moving parts or getting flung far away because a part teleported into them. There are ways to mitigate this, but implementing custom physics with CFrame is a major hack. It doesn’t provide any real benefits, and is a massive headache.

Use legacy body movers:
Old physics objects like BodyGyro/BodyPosition are no longer being actively supported and can have unpredictable behavior.

Solution:

To dictate where the minecart should travel, I use the node system suggested by @SteadyOn earlier. If you look at the track below, you can see green dots. These are attachments which I use to designate the next node the cart should travel to:

To move the Minecart, I use AlignOrientation/AlignPosition. These are normally used for keeping an object in-place rather than moving it, so I connected them to a proxy part which I move with CFrame. The proxy part being moved in the following video is pink, and the minecart is attached to that pink part with AlignOrientation/AlignPosition.

With this, you get the control of CFrame, but the benefits of physics simulation at the same time. In the following in-game (with latency) video, the minecart’s network ownership is set to the player sitting in the cart. The video is recorded from the perspective of the player standing on the cart.

Note that because the minecart’s location is being controlled by CFrame, it does not speed up or slow down on slopes. This was intentional for my use case, as I wanted to be able to build tracks without worrying if the minecart had enough speed to get to the end – it just goes the same speed always and predictably. If you wanted a more accurate physics simulation, you could probably use a different set of constraints.

154 Likes

Thanks so much! Especially for all the details.

2 Likes

I know I won’t stop asking so many questions, but how did you get the cart to always stay above the node? I’ve been messing around for over an hour and can’t replicate that. It usually drags a bit behind before moving which makes it look weird. It’s also getting stuck after a short time again.

1 Like

The attachment for the pink part, which follows the nodes, is at its center, but the attachment for the minecart is below the root part the AlignPosition connects to:

The AlignPosition keeps the root part’s attachment in the same position as the pink part’s attachment, and since the root part is a stud above its attachment, it gets held a stud above the pink part.

7 Likes

Alright, thanks. What exact settings does the AlignPosition have? Like I said, mine lags behind my node.

You need to enable RigidityEnabled on both the AlignPosition and AlignOrientation.

4 Likes

Thank you, again. Now the cart works perfectly until I sit in it, in which case it flings around everywhere. I tried using custom physical properties to make players weightless when they sit in it to no avail.

Sorry if this is getting annoying, but once again some help would be appreciated.

Can you upload a rbxl file here with the cart? I can take a look at it.

Minecart.rbxl (26.5 KB)

Ok, thanks for taking the time to do that.

1 Like