#car Auto-braking

1 messages · Page 1 of 1 (latest)

tardy walrus
#
  • I ran into a problem: when a player exits a car while it's moving, it continues to coast, but I wanted it to stop (either instantly, or at least try to brake, somehow).
  • More info in the next message, otherwise it's over the limit.
wait(0.2)

local seat = script.Parent
local car = seat.Parent

if not car then
    warn("Autostop: Car not found!")
    return
end

local wheels = car:WaitForChild("Wheels", 5)
if not wheels then
    warn("Autostop: Wheels not found!")
    return
end

local BRAKE_FORCE = 15000
local MIN_SPEED = 0.5

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    if not seat.Occupant then

        seat.Throttle = 0
        seat.Steer = 0
        
        local brakeForce = Instance.new("BodyForce")
        brakeForce.Name = "AutoBrake"
        brakeForce.Parent = seat
        
        task.spawn(function()
            while brakeForce and brakeForce.Parent do
                local velocity = seat.AssemblyLinearVelocity
                local speed = velocity.Magnitude
                
                if speed < MIN_SPEED then

                    brakeForce:Destroy()
                    break
                end
                
                local brakeVector = velocity.Unit * -BRAKE_FORCE
                
                    brakeForce.Force = brakeVector
                    
                task.wait(0.1)
            end
        end)
    end
end)
#

...
I'm just getting started with RB Studio and Lua in general, so I know next to nothing.

  • The car itself is made using A-Chassis.
  • I tried implementing it using physical vector counteraction, by anchoring car parts, by simulating the player pressing the brakes. I also tried using hints from the built-in AI, but it seems to be working like crap. I've settled on the vector counteraction method now, because at least it works IN PRINCIPLE, but
  1. the faster the car goes, the worse it works.
  2. when the car slows down, if it hits a random timing, it starts to "pendulum" back and forth.