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.
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.