How to make cart slowly accelerate and decelerate?

I am currently making a cart ride cart with working suspension, however when the cart begins moving it starts moving straight away, rather than accelerating up to the max speed like an ordinary car, heres my original code which moves the cart back and forward:


local seat = cart.VehicleSeat

local backLeft = cart.LB
local backRight = cart.RB

local frontLeft = cart.LF
local frontRight = cart.RF

local speed = 35

seat:GetPropertyChangedSignal("Throttle"):Connect(function ()
	
	frontLeft.Wheel.WheelContraint.AngularVelocity = -speed * seat.Throttle
	frontRight.Wheel.WheelContraint.AngularVelocity = speed * seat.Throttle
	
	backLeft.Wheel.WheelContraint.AngularVelocity = -speed * seat.Throttle
	backRight.Wheel.WheelContraint.AngularVelocity = speed * seat.Throttle
	
end)

As you can see by the recording the moving is very janky and if spammed can even throw the player of the track!

If you know how to fix this, it would help alot!

1 Like

The easiest way to achieve what you’re looking for is by using interpolation

@sleitnick posted a well-constructed tutorial for cars, which I recommend you follow. Here’s part 4 of his multi-part series, where he goes over the client script. You don’t need to follow it exactly but he uses interpolation in order to achieve that smooth acceleration and deceleration, which should solve your issue!

If you have any questions or concerns, don’t hesitate to ask!

1 Like

i dont know whether i have done it correctly, heres a video to show you if i have followed it correctly aswell with the new script, it doesnt look like it has changed anything?


local seat = cart.VehicleSeat

local backLeft = cart.LB
local backRight = cart.RB

local frontLeft = cart.LF
local frontRight = cart.RF

seat:GetPropertyChangedSignal("Throttle"):Connect(function ()
	
	local throttle = seat.ThrottleFloat
	local torque = seat.Torque
	local speed = seat.MaxSpeed * throttle
	
	frontLeft.Wheel.WheelContraint.MotorMaxTorque = torque
	frontRight.Wheel.WheelContraint.MotorMaxTorque = torque

	backLeft.Wheel.WheelContraint.MotorMaxTorque = torque
	backRight.Wheel.WheelContraint.MotorMaxTorque = torque
	
	frontLeft.Wheel.WheelContraint.AngularVelocity = -speed 
	frontRight.Wheel.WheelContraint.AngularVelocity = speed 
	
	backLeft.Wheel.WheelContraint.AngularVelocity = -speed
	backRight.Wheel.WheelContraint.AngularVelocity = speed
	
end)```
1 Like

It hasn’t changed because you didn’t change the speed part

His speed handler looks like this:

local ThrottleGoal = Seat.ThrottleFloat
Throttle += (ThrottleGoal - Throttle) * math.min(Delta * Seat.TurnSpeed, 1)
local Torque = Seat.Torque
local Speed = Seat.MaxSpeed * Throttle

Converting this to your script would look something like this

local RunService = game:GetService("RunService")

local Seat = Cart.VehicleSeat

local BackLeft = cart.LB
local BackRight = cart.RB

local FrontLeft = cart.LF
local FrontRight = cart.RF

local Throttle = 0

RunService.RenderStepped:Connect(function(Delta)
    local ThrottleGoal = Seat.ThrottleFloat
    Throttle += (ThrottleGoal - Throttle) * math.min(Delta * Seat.TurnSpeed, 1)
    local Torque = Seat.Torque
    local Speed = Seat.MaxSpeed * Throttle

    -- apply the speed & torque to the wheels here
end)

The whole point of interpolation is to achieve a value smoothly, which is why you add to the throttle incrementally (to get the smooth acceleration & deceleration). You can also print the Throttle value in order to see it go up/down smoothly, if you wish

:white_check_mark: Don’t forget to mark this post as a solution if this helped!

1 Like

sorry for the late responce, doesnt the renderstepped have to be a local script?

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.