Hello guys! I had been working on a raycast car suspension but the car go bouncing like crazy
for i , e in pairs(script.Parent:GetDescendants()) do
if e:IsA("BasePart") then
if e~= script.Parent.PrimaryPart then
local weld = Instance.new("WeldConstraint",script.Parent.PrimaryPart)
weld.Part1 = e
weld.Part0 = script.Parent.PrimaryPart
e.Massless = true
end
end
end
local FrontWheels = script.Parent.Wheel.F
local BackWheels = script.Parent.Wheel.B
local Setting = {
SuspensionMaxLength = 5,
SuspensionStiffness =100,
SuspensionDamper = 50,
WheelRadius = 1.5
}
for i , e in pairs(script.Parent.Wheel:GetDescendants()) do
if e:IsA("BasePart") then
local LengthValue = Instance.new("NumberValue",e)
LengthValue.Name = "SuspensionLength"
LengthValue.Value = Setting.SuspensionMaxLength
end
end
while true do
local delta = game["Run Service"].Heartbeat:Wait()
for i , e in pairs(script.Parent.Wheel:GetDescendants()) do
if e:IsA("BasePart") then
local LengthValue = e:FindFirstChild("SuspensionLength") or Instance.new("NumberValue",e)
LengthValue.Name = "SuspensionLength"
local prams = RaycastParams.new()
prams.FilterDescendantsInstances = {script.Parent,game.Workspace.Camera}
local Origin = e.Position
local Direction = e.CFrame.UpVector*-3
local ray = workspace:Raycast(Origin,Direction,prams)
-- visual--
local EndPos = Instance.new("Part",game.Workspace.Camera)
EndPos.Material = Enum.Material.Neon
EndPos.Size = Vector3.new(0.5,0.5,0.5)
EndPos.Anchored = true
EndPos.CanCollide = false
EndPos.Position = ray and ray.Position or Origin+e.CFrame.UpVector* -(Setting.SuspensionMaxLength + Setting.WheelRadius)
EndPos.Color = ray and Color3.new(1, 0, 0) or EndPos.Color
game.Debris:AddItem(EndPos,0.04)
if ray then
local Distance = (Origin-ray.Position).Magnitude
local SpringLength = math.clamp(Distance-Setting.WheelRadius,0,Setting.SuspensionMaxLength)
local StiffnessForce = Setting.SuspensionStiffness*(Setting.SuspensionMaxLength-SpringLength)
local DamperForce = Setting.SuspensionDamper *((LengthValue.Value-SpringLength))*delta
local SuspensionForceVec3 = script.Parent.PrimaryPart.CFrame.UpVector *(StiffnessForce+DamperForce)
LengthValue.Value = SpringLength
print(SpringLength)
--print(SuspensionForceVec3)
script.Parent.PrimaryPart:ApplyImpulseAtPosition(SuspensionForceVec3,ray.Position)---:ApplyImpulse(-SuspensionForceVec3 *script.Parent.PrimaryPart.AssemblyMass)
--:ApplyImpulseAtPosition(SuspensionForceVec3,ray.Position)-
end
end
end
end
here is the code . Plz help
Here is the car model