Help with a hovering block

So I am currently trying to script a car with suspension. The first thing I have to do is get a part to hover above the ground.

Screen Shot 2020-05-31 at 10.17.57 AM

I have to use a body-thruster in each corner that will hold the parts corner x studs above the ground. The reason I need one in each corner is to achieve the suspension (aka if the right tire is on a rock.) Every frame, I update the thrusters force with what’s necessary to keep it up however I’m having trouble getting the correct force and it just bounces up and down. I also get the distance to the ground in every corner every frame and I am hoping to use that in order to find the force, but I’m not sure what to use.

Also if body thruster is not the way to go to make a hovering block let me know.

Here’s my code:

local MainPart = script.Parent
function runSuspension()
	local studsAboveGround = 5
	
	for i = 1,4 do --For every corner thruster
		local RayOrigin = MainPart["Attachment"..i].WorldPosition --Gets the corners position
		local RayEnd = Vector3.new(RayOrigin.X,RayOrigin.Y-15,RayOrigin.Z)
		local ray = Ray.new(RayOrigin, RayEnd-RayOrigin)
		local hitPart, hitPosition = workspace:FindPartOnRay(ray, MainPart)
		local distanceToGround = (RayOrigin-hitPosition).magnitude --Gets the distance to the ground for that corner (Printed & Works)
	 
		script.Parent["BodyThrust"..i].Force = Vector3.new(0,?,0) --Apply the force in the corner to keep it up
	end
end
game:GetService("RunService").Heartbeat:Connect(function()
	runSuspension()
end)

I am not sure what force to put in the BodyThrust. I was thinking that it would only go in the Y, but I am now thinking it would be a combination of all numbers.

2 Likes

This video explains how you can use thrusters on each corner for suspension (Skip to 2:03)

Basically, you have to determine the compression ratio from the distance of the end point from the origin, which is a number between 0 - 1. The less the compression ratio is, the more force you’ll apply to the thruster:

local cr = distanceToGround / 15
local f = (1 - cr) * MAX_FORCE
script.Parent["BodyThrust"..i].Force = Vector3.new(0,f,0)

You’d just need to specify the MAX_FORCE you want to apply

4 Likes

How could I find the correct MAX_Force to keep it stable? It either overshoots or undershoots

2 Likes