How can I make this train follow a curved path?

I have this train someone made for me. I’m trying to make it follow these railroad tracks that curve. How would I make it follow the path along with the train cars?
I can’t think of anyway to move it in a path.

14 Likes

To make it go curve, you would need some kind of function. A circle would be quite easy, but I assume you curves won’t all be in the shape of a circle. Find what works to make it curve in what way you want.

Bezier Curves could be a useful way to make it curve.

5 Likes

That’s very helpful. But I still need to figure out how to move it along the path.

3 Likes

What kind of path do you want to create?
So far you’ve only shown us a straight path.

1 Like

This is the path I want it to take. Sorry for not showing the path earlier.

5 Likes

SetPrimaryPartCFrame is a good way to move one of the train cars, but shouldn’t be used for the whole thing. Move each one individually, create some train cars later or make some cars spawn further back so they move to the start. (like jailbreak)

Example of SetPrimaryPartCFrame

local RunService = game:GetService"RunService"
local function movemodel(model,start,End)
    for i=0,1,0.01 do
        if not model.PrimaryPart then break end
        model:SetPrimaryPartCFrame(start:Lerp(End,i))
        RunService.Heartbeat:Wait()
    end
end
--model under workspace (make sure it has a primarypart)
movemodel(workspace.Model,CFrame.new(),CFrame.new(100,0,100))
9 Likes

Thank you so much for the tips!

3 Likes

Could you not use TweenService here?

2 Likes

place nodes on the tips of the curves on your tack and compile them into a table. then use cos/sin to tween a control block the rest of your train follows based off constraints or whatever in a curve while traveling to the next point. doing it this way might be more tedious, but is funner.

3 Likes

You would have to tween every single part, which is a lot of tweens.

2 Likes

Unless you have a part that you choose to tween, and have the rest of it welded to that part / connected with AlignPosition, AlignOrientation and have it all unanchored other than the tweening part.

Example: How do I make my minecart stay on the rails?

3 Likes

Even if you used those, don’t tweens only work linearly?

i.e

game:GetService"TweenService":Create(workspace.Part,TweenInfo.new(1),{CFrame = CFrame.new(0,100,0)}):Play()

This would only move the part in a linear path. Does TweenService support non linear paths?

3 Likes

You’d have to do it using nodes for curves, but that applies whether you use SetPrimaryPartCFrame with lerp, CFrame every part or Tween it. You can’t make it follow a curved path without having nodes, so you tween from one node to the next.

3 Likes

You don’t have to use lots of nodes to make a curve. As I recommended before, you can use a function to create a curve. (ex: y = x^2)

Bezier Curves provide a relatively simple way to do this.

1 Like

I have made it move. Turning just looks funky. I am probably not setting my nodes how they are supposed to be. I put a node with the same orientation as the track wherever the railroad turns.

5 Likes

Do you adjust for the change in rotations? Lerping with CFrames will automatically adjust rotation along with the position.

1 Like

Depends on how you produce the CFrame input. Tween will do the rotation to if you provide it with a CFrame that has rotation. I use it for cars and trains.

It may be how you’ve attached the rest of the train to the tracking parts. Did you use alignposition and alignorientation or a weld or something?

3 Likes

I used a lot of welds. Each part in each car is welded to the PrimaryPart of that car, and then I Lerp the PrimaryPart’s CFrame to each node.

2 Likes

I did not. I’ll have to do that. Should it adjust right after or before the track turns?

2 Likes

The lerp should do the rotation if you use the full CFrame of the node as the goal, and not just the position part of it. Mind sharing the code with your inputs to the lerp function and setting the primary part to the result? Might help isolate the issue.

Also if your nodes are physical parts, make them visible to double check the orientation. If they are virtual then perhaps share the code that generated them to double check orientation is correctly set there too.

2 Likes