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)
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))
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.
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.
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.
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.
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?
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.
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.
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)
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.
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