So, i am trying to build a game inspired by an annoyingly long monorail game, but instead of monorails its trains
I have an issue with my train, it keeps derailing even on the straightest of straights!
How would i fix this?
Train driver if it helps:
Train Driver
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local train = script.Parent
local seat = train:FindFirstChild("VehicleSeat", true)
local ACCELERATION = 100
local ACCEL_RAMP_SPEED = 2.5
local WHEEL_RADIUS = 1.5
local SUSPENSION_TRAVEL = 1
local DAMPING_RATIO = 0.75
local NATURAL_FREQUENCY = 2
local currentSpeed = 0
local currentAccel = 0
local motors = {}
local springs = {}
local baseParts = {}
local totalMass = 0
for _, d in train:GetDescendants() do
if d:IsA("BasePart") then
totalMass += d:GetMass()
table.insert(baseParts, d)
if d.Parent.Name == "Wheel" then
d.CustomPhysicalProperties = PhysicalProperties.new(1, 0.1, 0.1, 1, 1)
end
elseif d:IsA("SpringConstraint") then
table.insert(springs, d)
elseif d:IsA("HingeConstraint") and d.ActuatorType == Enum.ActuatorType.Motor then
table.insert(motors, d)
end
end
local massPerSpring = totalMass / #springs
local angularFreq = 2 * math.pi * NATURAL_FREQUENCY
local stiffness = massPerSpring * angularFreq ^ 2
local damping = 2 * massPerSpring * angularFreq * DAMPING_RATIO
local freeLength = SUSPENSION_TRAVEL * 2
print(string.format(
"Springs: %d | Mass/spring: %.2f | Stiffness: %.1f | Damping: %.1f | FreeLength: %.2f",
#springs, massPerSpring, stiffness, damping, freeLength
))
for _, spring in springs do
spring.Stiffness = stiffness
spring.Damping = damping
spring.FreeLength = freeLength
end
local function setOwnership(player)
for _, part in baseParts do
part:SetNetworkOwner(player)
end
end
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
local occupant = seat.Occupant
if occupant then
local player = Players:GetPlayerFromCharacter(occupant.Parent)
if player then setOwnership(player) end
else
setOwnership(nil)
currentSpeed = 0
currentAccel = 0
for _, motor in motors do
motor.AngularVelocity = 0
end
end
end)
RunService.Heartbeat:Connect(function(dt)
local throttle = seat.Occupant and seat.Throttle or 0
currentAccel += (throttle * ACCELERATION - currentAccel) * (dt * ACCEL_RAMP_SPEED)
currentSpeed += currentAccel * dt
local angularVelocity = currentSpeed / WHEEL_RADIUS
for _, motor in motors do
motor.AngularVelocity = angularVelocity
end
end)