I’m making a script-based tank track, and when I test the game I notice that on certain curves the track link kind of changes the direction it’s pointed in, it has to be pointed inwards not outwards
this is my script
local RunService = game:GetService(“RunService”)
local enableRotationInterpolation = true
local function getPathPoints()
local pathPoints = {}for _, part in ipairs(script.Parent.Paths:GetChildren()) do
if part:IsA(“BasePart”) and tonumber(part.Name) then
table.insert(pathPoints, {position = part.Position, name = tonumber(part.Name)})
end
endtable.sort(pathPoints, function(a, b)
return a.name < b.name
end)local orderedPositions = {}
for _, point in ipairs(pathPoints) do
table.insert(orderedPositions, point.position)
endreturn orderedPositions
endlocal function calculatePathDistances(pathPoints)
local distances = {0}
for i = 2, #pathPoints do
local dist = (pathPoints[i] - pathPoints[i-1]).Magnitude
table.insert(distances, distances[i-1] + dist)
end
return distances
endlocal function moveLinks(links, pathPoints, distances, speed)
local pathLength = distances[#distances]for i, link in ipairs(links) do
local progress = (speed * tick() + (i - 1) * (pathLength / #links)) % pathLengthlocal index1, index2 for j = 1, #distances - 1 do if progress >= distances[j] and progress < distances[j + 1] then index1, index2 = j, j + 1 break end end local segmentLength = distances[index2] - distances[index1] local alpha = (progress - distances[index1]) / segmentLength local position = pathPoints[index1]:Lerp(pathPoints[index2], alpha) local direction = (pathPoints[index2] - pathPoints[index1]).Unit local orientation = CFrame.new(position, position + direction) local pivotOffset = link:GetPivot().Position - link.Position local pivotedCFrame = orientation * CFrame.new(pivotOffset) if enableRotationInterpolation then link.CFrame = link.CFrame:Lerp(pivotedCFrame, 0.1) else link.CFrame = pivotedCFrame end
end
endlocal function setupTankTracks()
local pathPoints = getPathPoints()
local distances = calculatePathDistances(pathPoints)local links = {}
for _, link in pairs(script.Parent:GetChildren()) do
if link.Name:match(“TrackLink”) then
table.insert(links, link)
end
endRunService.Heartbeat:Connect(function()
moveLinks(links, pathPoints, distances, 5)
end)
endsetupTankTracks()