I’ve got a solid script handling the locomotive movement, but I’m facing an issue where the bogies (trolleys) aren’t aligning with their nodes correctly. I need the first trolley needs to turn towards the next node, while the second should only turn towards the previous node. Correct turning of the trolleys is important to create realistic locomotive behavior.
Do you have some ideas about this?
local rs = game:GetService("RunService")
local train = script.Parent
local curSpeed = train.CurrentSpeed
local nodes = train.Parent.Parent.Nodes
local nodeReady = nodes.Parent.NodeReady
local loco = train.Locomotive
local bogie1 = loco.Model.B1
local bogie2 = loco.Model.B2
local front = loco.Front
local back = loco.Back
local base = loco.Base
local gap = (front.Position - back.Position).Magnitude
local loco_model = loco.Model
local startingNode = train.StartingNode
repeat wait() until nodeReady.Value
function SetStartingNode()
front.Current.Value = nodes:FindFirstChild("Node_" .. startingNode.Value)
back.Current.Value = nodes:FindFirstChild("Node_" .. startingNode.Value)
end
function NextNode(target)
local curNode = target.Current
local nextNode = curNode.Value.NextNode.Value
curNode.Value = nextNode
end
function Move(target, deltaTime)
local curNode = target.Current.Value
local nextNode = target.Current.Value.NextNode.Value
if not nextNode then
curSpeed.Value = 0
return
end
local distanceTraveledOnSection = (curNode.Position - target.Position).Magnitude
local sectionLength = (curNode.Position - nextNode.Position).Magnitude
local newAlpha = ((distanceTraveledOnSection + curSpeed.Value * deltaTime)) / sectionLength
target.CFrame = curNode.CFrame:Lerp(nextNode.CFrame, newAlpha)
if newAlpha >= 1 then
NextNode(target)
end
end
rs.Heartbeat:Connect(function(deltaTime)
Move(front, deltaTime)
Move(back, deltaTime)
base.CFrame = CFrame.new(back.Position, front.Position) * CFrame.new(0,front.Size.Y/2 + base.Size.Y/2,-gap/2)
loco_model:SetPrimaryPartCFrame(base.CFrame * CFrame.new(0,base.Size.Y/2 + loco_model.PrimaryPart.Size.Y/2,0))
end)
SetStartingNode()
Alright, so you said the locomotive moves fine right? As i’ve seen from the script you havent implemented the trolley movement yet right? And your goal is to make the trolleys face the previous node. You could achieve this by storing the previous node in a table and make the trolley face it like you’ve done with your current script, also you’d have to make the amount of previous nodes that you have stored scale with the amount of trolleys that you have. Could you maybe provide a video of the train moving to get more clarification so we can work out a script?
Okay. Here is a small video fragment showing the behavior of the locomotive and, of course, the inactivity of the trolleys, which must turn according to where the rail under them goes:
Alright so what I would do is add a previous node value to the nodes so you can retrace your steps, then we get the current node of the back and the distance of it from the trolleys front node. Now we add a loop which goes back the previous nodes. In here we add a counter which adds the distance of every node to the previous node and when the distance is greater or equal to the distance of the trolleys node we select that node as the current one and do the same steps in moving it as youve already done
Well, by the way, I’ve already done it but it seems to cframe changes very strangely!
This is still a demo version, that is, later it is necessary to do with smooth animation of movement with using lerp.
local bogie1Pos, bogie2Pos = bogie1:GetPrimaryPartCFrame().Position, bogie2:GetPrimaryPartCFrame().Position
local frontPos, backPos = front.Current.Value.Position, back.Current.Value.Position
bogie1:SetPrimaryPartCFrame(CFrame.new(bogie1Pos, Vector3.new(frontPos.X, bogie1Pos.Y, frontPos.Z)))
bogie2:SetPrimaryPartCFrame(CFrame.new(bogie2Pos, Vector3.new(backPos.X, bogie2Pos.Y, backPos.Z)))
Try this code. I think the weird movement is due to the LookVector of the CFrame causing your Bogie to look at the Y-Pos as well. This will make it look at the respective X, Z vectors for the Front/Back positions, but your the bogie’s current Y pos in the look vector. Basically saying, look at the node, but ignore the Y. However, in the future you’d have to make adjustments for hills.