How do I make my minecart stay on the rails?

(Can’t post pics right now because I’m on mobile)

I’m trying to make a minecart for my game, and while I can move it just fine, I can’t get it to stay on the rails (Which right now is just a block beneath it w/ velocity set).

I’ve tried to add invisible bricks beneath it that go under the “rails” like those old roller coasters used to do to stay on the track, but that doesn’t really work and if I try to make the cart take a turn it totally breaks.

Sorry if I’m being too vague, but I’d like some help before I go back to working on it again…

UPDATE: Here’s some pictures of what I’m talking about:

The minecart is being held onto the tracks with these red bricks:

This is what happens when it hits a turn:

Any ideas?

18 Likes

I think a picture of your current setup would help when you get the chance to post one.

I’m having a hard time picturing your setup and with the absence of pictures or a place file, it is impossible for anyone to give you any advice.

Please attach some visuals, files or other helpful information to your post in order to explain the situation, and refer to the ‘building support’ category guidelines in order to ensure that other developers can help you in a meaningful way.

2 Likes

Alright, I finally found time to upload some pictures.

3 Likes

I wouldn’t recommend doing it like this. If you are wanting to allow players to ride in it, use a BodyPosition tweening between nodes on the journey, that way the cart wont be touching anything and can’t get stuck, especially if you put it in a collision group only players can collide with.

Otherwise, it’s a lot easier, use CFrame to tween between nodes on the journey, it will still be interactable but players won’t be able to ride in it.

This is the same issue @badcc had with jailbreak trains, how to keep them on track and looking good but also interactable and ridable by players.

7 Likes

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