Help with Raycast Suspension

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.

How would I make it work with wheels? Like the wheels go under.

First, increase the damping since the wheels add mass. Second, you need to

Change this and the TargetPosition to distanceFromGround-3 (or your wheel radius).

Don’t subtract distanceFromGround itself, since it still influences the force.

Its very shaky.


Wheels are CanCollide off and Massless on

Turn massless off. You need to decrease the stiffness.

What does your code look like now?

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)
1 Like

Where’d this come from? I guess this is your stiffness, you need to decrease it.

image

model.VectorForce.Force = (chassis.CFrame.UpVector*(1/(distanceFromGround/.1))*mass*10)

Not sure why the wheel is going up, what is the minimum length for the wheel?

The stiffness looks like it’s working though.

image

Try changing LowerLimit to -1 and change Restitution to 0.

Same result.
image

Are you sure the wheels and lower parts have can collide set to false?

Yes. The lower parts and upper parts have massless if thats the problem.

Car Suspension Test.rbxl (46.3 KB)

1 Like

This isn’t the right file.

Try disabling that.

Car Suspension Test (2).rbxl (46.3 KB)

1 Like

I have tried. Also new file posted.

I don’t have the updated file, it doesn’t have wheels:

Car Suspension Test (1).rbxl (46.3 KB)
Car Suspension Test (2).rbxl (46.3 KB)
I don’t know. Try these?

1 Like