Im trying to make a suspension system for my car using raycast. My car is bouncing too much and when i increase dampness it just flips and it isnt stable no matter how much i change the values. heres the code:
local carBody = script.Parent.CarBody
local massPart = script.Parent.Mass
local springTravel = 2
local restLength = 3
local maxLength = restLength + springTravel
local minLength = restLength - springTravel
local springLength = restLength
local springStiffness = 1000
local damperStiffness = 50
local springForce
local damperForce
local suspensionForce
local springVelocity
local wheelRadius = 3.732/2
local wheelAttachments = {
Fl = carBody.AttachmentFL,
FR = carBody.AttachmentFR,
BL = carBody.AttachmentBL,
BR = carBody.AttachmentBR
}
local suspensionLengthMemory = {}
for i, v in pairs(wheelAttachments) do
suspensionLengthMemory[i] = 2
end
carBody:SetNetworkOwner(nil)
while true do
local delta = game["Run Service"].Heartbeat:Wait()
for wheelName, att in pairs(wheelAttachments) do
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {script.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.IgnoreWater = true
local rayOrigin = att.WorldPosition
local rayDirection = -carBody.CFrame.UpVector * (maxLength + wheelRadius)
local raycast = game.Workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycast then
local rayDistance = rayOrigin - raycast.Position
springLength = math.clamp(raycast.Distance - wheelRadius, minLength, maxLength)
springVelocity = (suspensionLengthMemory[wheelName] - springLength)/delta
springForce = springStiffness * (restLength - springLength)
damperForce = damperStiffness * springVelocity
suspensionForce = (springForce + damperForce) * att.CFrame.UpVector
carBody:ApplyImpulseAtPosition(suspensionForce, raycast.Position)
else
end
end
end
vid showing the problem:
External Media