Need help with raycast car suspension

Hello World!

I currently need help on fixing this car suspension code.

Bugs :

  • Floats in the air. But if interacted, it flies away.
  • Most of the time it just sinks, into the ground. If there is no collision.

Please help.

Here is the code :

Code

1st Module Script - [ Settings ]

local setting = {}

setting.restLength = script:GetAttribute("restLength")
setting.springTravel = script:GetAttribute("springTravel")

setting.springStiffness = script:GetAttribute("springStiffness")
setting.damperStiffness = script:GetAttribute("damperStiffness")

setting._minLength = 0
setting._maxLength = 0
setting._lastLength = 0
setting._springLength = 0

setting._springVel = 0

setting._springForce = 0
setting._damperForce = 0

setting._suspensionForce = Vector3.new()

setting.wheelRad = script:GetAttribute("wheelRad")

return setting

Attributes :

image

2st Module Script - [ Main one ]

local car = script.Parent.Parent:WaitForChild("Car")

local setting = require(script.Parent:WaitForChild("settingsModule"))

local suspensionModule = {}
suspensionModule.__index = suspensionModule

function suspensionModule.new()
	local self = setmetatable({}, suspensionModule)
	
	return self
end

function suspensionModule:suspension(minLength : minLength, maxLength : maxLength, DeltaTime : DeltaTime)
	local restLength = setting.restLength
	local springLength = setting._springLength
	local lastLength = setting._lastLength
	
	local springVel = setting._springVel
	
	local springTravel = setting.springTravel
	
	local damperStiffness = setting.damperStiffness
	local springStiffness = setting.springStiffness
	
	local springForce = setting._springForce
	local damperForce = setting._damperForce
	local suspensionForce = setting._suspensionForce
	
	local wheelRad = setting.wheelRad
	
	--// Gets wheel positions
	
	for _, v in pairs(car:GetChildren()) do
		if v:IsA("BasePart") then
			local dir = -v.CFrame.UpVector * (maxLength + wheelRad)

			local rayParams = RaycastParams.new()
			rayParams.FilterType = Enum.RaycastFilterType.Blacklist
			rayParams.IgnoreWater = true
			rayParams.RespectCanCollide = true

			rayParams.FilterDescendantsInstances = {car}

			local ray = workspace:Raycast(v.Position, dir)

			if ray and ray.Instance then
				lastLength = springLength
				springLength = ray.Distance - wheelRad
				springLength = math.clamp(springLength, minLength, maxLength)

				springVel = (lastLength - springLength) / DeltaTime

				springForce = springStiffness * (restLength - springLength)
				damperForce = damperStiffness * springVel

				suspensionForce = (springForce + damperForce) * car.CFrame.UpVector
				
				v.AssemblyLinearVelocity = suspensionForce
			end
		end
	end
end

function suspensionModule:init(DeltaTime : DeltaTime)
	local restLength = setting.restLength
	local springTravel = setting.springTravel
	local wheelRad = setting.wheelRad
	
	local minLength = setting._minLength
	local maxLength = setting._maxLength
	local springLength = setting._springLength
	
	minLength = restLength - springTravel
	maxLength = restLength + springTravel
	
	suspensionModule:suspension(minLength, maxLength, DeltaTime)
end

return suspensionModule

Thanks in Advance!