Raycast Suspension System Problem

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
1 Like

I’m not sure if you know, but there’s SpringConstraint class which you can use that would honestly handle all this for you

i know that but i wanna make with raycast.

Why? You really don’t need to if you just configure the SpringConstraint correctly, it absolutely is the better choice in every scenario and I know that having made vehicle systems myself.

i thought raycast suspensions were superior thats why ive been trying to make one. is that not the case?

I’m going to make a video for you showing my vehicle system that has SpringConstraints, you can be the judge if it’s worse or not

https://streamable.com/it7q0g

alr then imma stick to spring constraints. Thanks for helping

1 Like