Help with Raycast Suspension

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)

image

1 Like

I’m not sure how this really works, it looks like this just controls the suspension damping.

Does the car use physics to roll the wheels?

1 Like

I haven’t done that yet. Working on the suspension first.

1 Like

I’ve never seen a suspension system like this before. I’ve always calculated it using CFrames and a VectorForce.

Is the wheel going to touch the ground or is it going to have can collide off and just be there for visuals?

1 Like

So I use VectorForce and PrismaticConstraint?

1 Like

Yeah, I guess so. VectorForce just propels the car upward.

Unless you plan to use Roblox physics, of course.

So do I just use the PrismaticConstraint to align the parts?
image

I think you set it to the distance of the cast. Try anchoring the base and moving it with the move tool while in run mode.

image

I think it works; you just need to add forces to make it go upwards without it being anchored.

I’ve accomplished this using VectorForce on my chassis.

What do I put?

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

Try this:

--Set the VectorForce.Force to:
(Vector3.yAxis*(1/(distanceFromGround/3))*1e3)-(model.LowerPart:GetVelocityAtPosition(model.LowerPart.Position)*10)

replace the 1e3 with your car’s mass if you wish. Replace the 3 with the target position.

Doesn’t work:

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

Does it not push the car up or does the car disappear?

Check if the car is anchored and if the VectorForce’s attachment is set.

I just falls down. Everything is set.

Change this to the target length of the position, not the position itself.

I mean you change it to how much you want your car above the ground.

It errors.

LowerPart is not a valid member of Model "Workspace.Car.Suspensions.Suspension1"

Car Suspension Test.rbxl (46.3 KB)
File if you want to see.

1 Like

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.

Anyways, here’s the car now:
Car Suspension Test.rbxl (46.8 KB)

1 Like

Can you like add descriptions of what it does? I don’t really understand some of them.