I'm making a raycast suspension system - but i need to add dampering to stop it bouncing
I pinned the video of what I have so far
Module 📜 :
local Suspesnion = {}
--// Constants \\--
local springStiffness = 300
local restLength = 1.3
local springTravel = 0.5
local wheelRadius = 1.25
local Vertices = {
{1, 1, -1}, -- top front right
{-1, 1, -1},-- top front left
{1, 1, 1}, -- top back right
{-1, 1, 1} -- top back left
}
local function GetWheelPositions(Car : Part)
local Size = Car.Size
local Wheels = {}
for _, Vector in pairs(Vertices) do
table.insert(Wheels, (Car.CFrame * CFrame.new(Size .X/2 * Vector[1], Size .Y/2 * Vector[2], Size .Z/2 * Vector[3])).Position)
end
return Wheels
end
function Suspesnion.Suspesnion(car : Part)
local Wheels = GetWheelPositions(car)
for wheelName, OriginalPositon in pairs(Wheels) do
-- Raycast Settings
local Filters = { car }
local RaycastParams = RaycastParams.new()
RaycastParams.FilterDescendantsInstances = Filters
local hit = game.workspace:Raycast(OriginalPositon, -car.CFrame.UpVector, RaycastParams)
if hit then
local currentSpringLength = (hit.Distance - wheelRadius)
local springCompression = (restLength - currentSpringLength) / springTravel
local springForce = (springStiffness + springCompression)
car:ApplyImpulseAtPosition(springForce * car.CFrame.UpVector, OriginalPositon)
end
end
end
return Suspesnion
Server 📜 :
-- Setup
local Car = game.Workspace:WaitForChild("Car")
local SuspensionModule = require(script.Suspension)
game:GetService("RunService").PreSimulation:Connect(function()
SuspensionModule.Suspesnion(Car)
end)
Please someone help 🙏