-
What do you want to achieve? I am trying to create my very own custom scripted suspension system
-
What is the issue? The car falls to the ground because of gravity, adding any force or removing gravity will break the suspension system. multiplying the force will also break it
robloxapp-20230106-1516142.wmv (950.7 KB)
The spring is functioning properly… it’s just not pushing up against gravity
The script works on small parts though, 1x1x1 is the biggest a part can be before it falls to the ground though
Here is the code:
local d = 6 -- Suspension Distance
local a = 1.8 --Stiffness, higher means higher height at suspension
local b = 0.6 --Rigidity higher means the suspensions will get less effected by slight changes and will balance out unlike Roblox's suspensions. This is the part that dampens the suspension.
local ST = {{dx=0,d2x=0}, {dx=0,d2x=0}, {dx=0,d2x=0}, {dx=0,d2x=0}} -- FL, FR, RL, RR
function Spring(suspension_distance, STN)
local x = d - suspension_distance --X is how much the suspension is contracted
local f = a*x + b*(x-ST[STN].dx) + b/2*(x-ST[STN].d2x) --Use the equation
ST[STN].d2x = ST[STN].dx --Store the past values
ST[STN].dx = x
return f
end
local params = RaycastParams.new()
params.FilterDescendantsInstances = {script.Parent}
while wait() do
for i,v in pairs(script.Parent.Forces:GetChildren()) do
local pos = v.Attachment0.WorldPosition
local cast = workspace:Raycast(Vector3.new(pos.X, pos.Y + 0.1, pos.Z), Vector3.new(0, -100, 0), params)
if not cast then continue end
local dist = (pos - cast.Position).Magnitude
local f = Spring(dist, i)
v.Force = Vector3.new(0, f, 0)
end
end
The script is an adapted version of this