Hello! I am trying to develop a car handling system for my racing game, I wanted to add sway bars to my car, so I picked this way of implementing it but I wasn’t able to do it the car still flips in sharp turns randomly flings, etc. The script on the link is written for unity I tried my best to turn it into a Roblox script
wheel pos = ray origins
hit pos = rayResult.Position s
local function travelCalc(tekerPos,HitPos)
local a = CFrame.new(tekerPos) * (carCenterMassPart.CFrame - carCenterMassPart.Position)
local x=(a:ToObjectSpace(CFrame.new(HitPos))).Y*-1
local y=(x-wheelRad)/maxSpringLenght
return y
end
local function rollBarFunc(dt)
local travelL=1
local travelR=1
rollBar = car.Config.Suspension.RollBar.Value
if (isAir[1] == false) then
travelL = travelCalc(wheelPoses[1],hitPoses[1]);
end
if (isAir[2] == false) then
travelR = travelCalc(wheelPoses[2],hitPoses[2]);
end
local antiRollPower = (travelL - travelR) * rollBar;
if isAir[1] == false then
carCenterMassPart:ApplyImpulseAtPosition(carCenterMassPart.CFrame.UpVector*-1*antiRollPower, hitPoses[1] + (wheelRad * carCenterMassPart.CFrame.UpVector));
end
if isAir[2]==false then
carCenterMassPart:ApplyImpulseAtPosition(carCenterMassPart.CFrame.UpVector* antiRollPower, hitPoses[2] + (wheelRad * carCenterMassPart.CFrame.UpVector));
end
--
travelL=1
travelR=1
rollBar = car.Config.Suspension.RollBar.Value
if (isAir[3] == false) then
travelL = travelCalc(wheelPoses[3],hitPoses[3]);
end
if (isAir[4] == false) then
travelR = travelCalc(wheelPoses[4],hitPoses[4]);
end
antiRollPower = (travelL - travelR) * rollBar;
if isAir[3] == false then
carCenterMassPart:ApplyImpulseAtPosition(carCenterMassPart.CFrame.UpVector*-1*antiRollPower, hitPoses[3] + (wheelRad * carCenterMassPart.CFrame.UpVector));
end
if isAir[4]==false then
carCenterMassPart:ApplyImpulseAtPosition(carCenterMassPart.CFrame.UpVector* antiRollPower, hitPoses[4] + (wheelRad * carCenterMassPart.CFrame.UpVector));
end
end
did I get the calculation wrong or something any help would be appreciated
no I won’t lover the center of the mass to an unrealistic point nor add weight to the car because it makes the car behave awkerdly