The vector force is now relative to each thruster. By raycasting downwards, we can get the force. The damping force is a subtracted force to prevent the car from being “bouncy”, and the ApplyAtCenterOfMass being false allows it to be relative to the thruster itself.
All of this works together to push the car upwards at the lowest point by taking the reciprocal of the distance. If the distance is .1, we will get 10. However, we also can divide the distance by .1, which makes distance bigger, and in return the force is less. We take this force and multiply it by mass and gravity, which are the required forces to push an object upwards in the Roblox engine.
Then, we subtract velocity (damping) from the force to create a smooth experience for the driver. If we didn’t do this, the car would be bouncy.
local vehicle = script.Parent
local chassis = vehicle.Chassis
local Suspensions = vehicle:WaitForChild("Suspensions")
local RunService = game:GetService("RunService")
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {vehicle}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local function getMass(model)
local mass = 0
for i,v in ipairs(vehicle:GetDescendants()) do
if v:IsA("BasePart") then
mass += v.AssemblyMass
end
end
return mass
end
local function raycast()
for _, model in ipairs(Suspensions:GetChildren()) do
if model:IsA("Model") then
local mass = getMass(model)
local rayOrgin = model.UpperPart.Position
local rayDirection = -chassis.CFrame.UpVector*10
local raycastResult = workspace:Raycast(rayOrgin, rayDirection, raycastParams)
local distanceFromGround = 0
local damping = (model.UpperPart:GetVelocityAtPosition(model.LowerPart.Position)*500)
model.VectorForce.Force = Vector3.zero
if raycastResult then
distanceFromGround = (rayOrgin - raycastResult.Position).Magnitude
if distanceFromGround > 0 then
model.UpperPart.PrismaticConstraint.LowerLimit = distanceFromGround-5
model.UpperPart.PrismaticConstraint.TargetPosition = distanceFromGround-5
model.VectorForce.Force = (chassis.CFrame.UpVector*(1/(distanceFromGround/.1))*mass*2000)
end
end
model.VectorForce.Force -= damping
end
end
end
RunService.Heartbeat:Connect(function()
raycast()
end)