Raycast Car Bouncing Problem

Im trying to make my custom raycast car but the problem is the car just bounces uncontrollably

local x = script.Parent.Size.X/2
local y = script.Parent.Size.Y/2
local rayparam = RaycastParams.new()
rayparam.FilterType = Enum.RaycastFilterType.Blacklist
rayparam.FilterDescendantsInstances = {script.Parent}
local AllCarsMemory = {}
AllCarsMemory[script.Parent] = {}--adds table for any memory you want, but i need it only for suspension right now
AllCarsMemory[script.Parent].SpringLengthMemory = {
	{.5,Vector3.new(x,0,y)},
	{.5,Vector3.new(-x,0,y)},
	{.5,Vector3.new(x,0 ,-y)},
	{.5,Vector3.new(-x,0,-y)}}--adds table for springs length memory, values added below
game:GetService("RunService").Heartbeat:Connect(function(delta)
	local DisCarSpringLength = AllCarsMemory[script.Parent].SpringLengthMemory
	local carCFrame = script.Parent.CFrame
	for i,v in pairs(AllCarsMemory[script.Parent].SpringLengthMemory) do
		local ray = workspace:Raycast(carCFrame:ToWorldSpace(CFrame.new(v[2])).Position,-carCFrame.UpVector*5,rayparam)
		if ray then
			local SpringLength = math.clamp(ray.Distance - 2, 0, 5)
			local StiffnessForce = script.Parent:GetAttribute("Stiffness") * (5 - SpringLength)
			local DamperForce = script.Parent:GetAttribute("Damper") * (( DisCarSpringLength[i][1] - SpringLength) / delta)
			local SuspensionForceVec3 = carCFrame.UpVector * (StiffnessForce+DamperForce)
			local RotationsOnlyWheelDirCFrame = CFrame.lookAt(Vector3.zero,carCFrame.LookVector,carCFrame.UpVector)
			local LocalVelocity = RotationsOnlyWheelDirCFrame:ToObjectSpace(CFrame.new(script.Parent:GetVelocityAtPosition(ray.Position)))
			local Xforce = carCFrame.RightVector * -LocalVelocity.x
			local Zforce = carCFrame.LookVector * LocalVelocity.z
			script.Parent:ApplyImpulseAtPosition(SuspensionForceVec3+Xforce+Zforce,ray.Position)
		end
	end
end)
1 Like