How can I make this train follow a curved path?

Its best to adjust it along the way.

local part = Instance.new("Part")
part.Anchored = true
part.CFrame = CFrame.new()
part.Parent = workspace
local CF1 = CFrame.new()
local CF2 = CFrame.new(0,10,0)*CFrame.Angles(0,math.pi/2,0)
for i=0,1,0.01 do
    part.CFrame = CF1:Lerp(CF2,i)
    wait()
end

Lerping CFrames instead of Vector3s means you don’t have to worry about updating the position and the rotation separately.

3 Likes

Edited code so people don’t steal. :eyes:

function GetTime(Distance, Speed)
	local Time = Distance / Speed
	return Time
end
function movemodel(model,start,End,AddBy)
    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
local Distance = (script.Parent.PrimaryPart.Position - nextnode.Position).magnitude 
local Time = GetTime(Distance, Speed) -- Calculate the time
local AddBy = 1/Time
movemodel(script.Parent,start,End,AddBy)
2 Likes

That looks like it should work. I’d double check your nodes are the correct orientation by making them visible for the moment, and possibly output the angles of start, End and the lerp result for debugging - if they all show the correct values then something else is going on.

1 Like

Thank you so much for the replies @Halalaluyafail3 @BanTech @marinatedsushi. Finally got it to curve the right way. :grin:

7 Likes

How did you manage to follow the railroad the right way? I tested your edited code, but it won’t work. Mind sharing the not edited code with me? I can do better with your code. I am a developer, I wish to join you in your game xCrim

You can adjust the orientation as exactly as the railroad turns. CFrame contains position and rotation

This is an old post. I ended up making a much better version after I left the XCrim Dev Team. But I haven’t been able to get on studio in a week or so.

2 Likes

How did you manage to make the train go straight and follow curves? Mind sharing your undedited code because i tried it and before i can reply to you i was not a new member yet. Can you share your non edited code with me?

Been a while. I’ll post this file here because I’ve been getting Discord dms about it.
In this file it’s a minecart instead of a train but it’s still the same concept. You can try converting it into a train.
Trainstuff.rbxl (126.6 KB)

9 Likes

Is there any way I could add multiple train cars / carriages that are coupled to the main train in that file?

3 Likes

Easy. This file contains both the Train and minecart.
PiggysTrainSystem.rbxl (128.8 KB)

Not gonna make a tutorial on this. People need to learn to either make your own stuff or learn by looking at how the code is used.

19 Likes

I find a way to make something different.
Automatic Train System - Resources / Community Resources - Developer Forum | Roblox