I need help finding a way to prevent my “car” from sliding down hills. It should not slide at all, but it still slides by a very small amount.
This is similar to my desired result: Raycast Car - Roblox @scripted_pj
This is what my car is like now: Oasis - Roblox
Notice how it will slide slightly on hills
This is my current code:
local RunService = game:GetService("RunService")
local crawler = script.Parent
local springs = crawler["Car?"]
local seat = crawler.VehicleSeat
local forwardSpeed = 0
local turnSpeed = 0
local MaxSpeed = 10000
local MaxTurn = 3
local modelMass = 0
local Stiffness = 320
local Damper = 15
local WheelFriction = 400
local WheelSuspensionLengthMemory = {}
for i,v in pairs(springs:GetChildren()) do
WheelSuspensionLengthMemory[v.Name] = 0.5
end
for i,v in ipairs(crawler:GetDescendants()) do
if v:IsA("BasePart") then
modelMass += v.AssemblyMass
end
end
local function Suspension(raycastResult, spring, dt)
local SuspensionForce = spring:FindFirstChild("SuspensionForce")
if SuspensionForce then
if raycastResult then
local raycastDistance = (spring.Position - raycastResult.Position).Magnitude
local springLength = math.clamp(raycastDistance - 1, 0, 20)
local stiffnessForce = Stiffness * (20 - springLength)
local damperForce = Damper * ((WheelSuspensionLengthMemory[spring.Name] - springLength) / dt)
local totalForce = spring.CFrame.UpVector * (stiffnessForce + damperForce)
local rotOnlyDirFrame = CFrame.lookAt(Vector3.zero, spring.CFrame.LookVector, spring.CFrame.UpVector)
local locvel = rotOnlyDirFrame:ToObjectSpace(CFrame.new(crawler.PrimaryPart:GetVelocityAtPosition(raycastResult.Position)))
local rightForce = spring.CFrame.RightVector * -locvel.X * WheelFriction
if spring.Name == "LeftForward" or spring.Name == "RightForward" then
rightForce = spring.CFrame.RightVector * -(locvel.X - turnSpeed) * WheelFriction
end
local lookForce = spring.CFrame.LookVector * (locvel.Z + forwardSpeed) * WheelFriction
--SuspensionForce.Force = totalForce
crawler.PrimaryPart:ApplyImpulseAtPosition(totalForce + rightForce + lookForce, spring.Position)
WheelSuspensionLengthMemory[spring.Name] = raycastDistance
else
SuspensionForce.Force = Vector3.new()
WheelSuspensionLengthMemory[spring.Name] = 20
end
end
end
RunService.Heartbeat:Connect(function(dt)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {crawler}
if seat.Occupant then
forwardSpeed = seat.Throttle * 10
turnSpeed = seat.Steer * 10
end
for springName, spring in springs:GetChildren() do
local raycastResult = workspace:Raycast(spring.Position, -spring.CFrame.UpVector * 20)
if false then
local distance = (spring.Position - raycastResult.Position).Magnitude
local p = Instance.new("Part", workspace)
p.Anchored = true
p.CanCollide = false
p.Size = Vector3.new(0.1, 0.1, distance)
p.CFrame = CFrame.new(spring.Position, raycastResult.Position)*CFrame.new(0, 0, -distance/2)
p.Name = "raycast"
end
Suspension(raycastResult, spring, dt)
end
end)