So I’m trying to create a car using raycast as the suspension but I don’t know how it really works. I only know that they use PrismaticConstraint’s.
Am I doing this right?
local vehicle = script.Parent
local Suspensions = vehicle:WaitForChild("Suspensions") -- folder of suspensions
local RunService = game:GetService("RunService")
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {vehicle}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local function raycast()
for _, model in ipairs(Suspensions:GetChildren()) do
if model:IsA("Model") then
local rayOrgin = model.UpperPart.Position
local rayDirection = Vector3.new(0, -1000, 0)
local raycastResult = workspace:Raycast(rayOrgin, rayDirection, raycastParams)
if raycastResult then
local distance = raycastResult.Distance + 3
if distance > 0 then
model.UpperPart.PrismaticConstraint.LowerLimit = distance
model.LowerPart.Position = raycastResult.Position
end
end
end
end
end
RunService.Heartbeat:Connect(function()
raycast()
end)
local function raycast()
for _, model in ipairs(Suspensions:GetChildren()) do
if model:IsA("Model") then
local rayOrgin = model.UpperPart.Position
local rayDirection = Vector3.new(0, -1000, 0)
local raycastResult = workspace:Raycast(rayOrgin, rayDirection, raycastParams)
if raycastResult then
local distanceFromGround = (rayOrgin - raycastResult.Position).Magnitude
if distanceFromGround > 0 then
model.UpperPart.PrismaticConstraint.LowerLimit = distanceFromGround
model.LowerPart.VectorForce.Force = ??? <-----
end
end
end
end
end
local vehicle = script.Parent
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 = Vector3.new(0, -1000, 0)
local raycastResult = workspace:Raycast(rayOrgin, rayDirection, raycastParams)
if raycastResult then
local distanceFromGround = (rayOrgin - raycastResult.Position).Magnitude
if distanceFromGround > 0 then
model.UpperPart.PrismaticConstraint.LowerLimit = distanceFromGround
model.LowerPart.VectorForce.Force = (Vector3.yAxis*(1/(distanceFromGround/raycastResult.Position))*mass)-(model.LowerPart:GetVelocityAtPosition(model.LowerPart.Position)*10)
end
end
end
end
end
This is because it’s disappearing for some reason. I think the problem is the vector force is attached to the lower part, when it’s supposed to be for the upper part.
I made all vector forces parented to the suspension model, and they are relative to world.
ApplyAtCenterOfMass was true (I changed it to false for you), and I forgot to multiply the force by gravity. The damping was also way too low.