I’m making a car chasis with scripted suspension but whenever one of the wheels is elevated the car starts to drift, how can I avoid this?
This is the wheel code that handles suspension force for the VectorForce located in the wheel position.
local offsetCFrame: CFrame = self.attachment.CFrame
local wheelCFrame: CFrame = self.attachment.WorldCFrame
local wheelPosition: Vector3 = self.attachment.WorldPosition
local springDirection: Vector3 = wheelCFrame.UpVector
local suspensionVector: Vector3 = -springDirection * (suspensionLength + wheelRadius)
local raycastResult: RaycastResult = workspace:Raycast(wheelPosition, suspensionVector, self.parameters)
if raycastResult then
local wheelWorldVelocity = self.root:GetVelocityAtPosition(wheelPosition)
local raycastLength = (wheelPosition-raycastResult.Position).Magnitude
local offset = (suspensionLength+wheelRadius) - raycastLength
local wheelRollingVelocity = wheelCFrame.LookVector:Dot((wheelWorldVelocity))
local wheelSlideVelocity = wheelCFrame.RightVector:Dot(wheelWorldVelocity)
local springVelocity = springDirection:Dot(wheelWorldVelocity)
local resistance = -rollingResistance * wheelRollingVelocity
local friction = -frictionCoefficient * wheelSlideVelocity
local force = (offset*stiffness) - (springVelocity*damper)
local desiredWheelCFrame = offsetCFrame - Vector3.new(0, raycastLength-wheelRadius, 0)
self.weld.C0 = if self.rotated then desiredWheelCFrame*FLIP_ROTATION else desiredWheelCFrame
local appliedForce = Vector3.yAxis*force*self.root.AssemblyMass
appliedForce -= Vector3.zAxis*resistance
appliedForce += Vector3.xAxis*friction
self:setForce(appliedForce)
end