Vehicle acceleration script

I am creating a cart ride game and I’m using a custom script combined with the vehicle seat. As of right now the acceleration and braking is instant and instead of rolling to a stop when nothing is being pressed down, the cart acts like it is braking. How can I make the acceleration/braking smooth and also have the cart roll to a stop when not pressing any keys?

My script:

local speed = script.Parent.Speed
local seat = script.Parent.Parent.Parent.Body.VehicleSeat
local maxspeed = seat.MaxSpeed

while true do
	wait(0.1)
	local look = script.Parent.CFrame.lookVector * script.Parent.Speed.Value
	
	if seat.Throttle == 1 then
		script.Parent.BV.velocity = Vector3.new(look.x, 0, look.z)
	elseif seat.Throttle == 0 then
		script.Parent.BV.velocity = Vector3.new(0, 0, 0)
	elseif seat.Throttle == -1 then
		script.Parent.BV.velocity = Vector3.new(-look.x, 0, -look.z)
	end
end

Instead of using a Velocity, you’ll want to use a BodyForce (assuming you’re using the deprecated system)
A velocity is a velocity, set the velocity to 0 and the velocity is 0. Acceleration = Force / Mass, so add force to get acceleration.

2 Likes

Well, besides your problem, I shortened your code.

local speed = script.Parent.Speed
local seat = script.Parent.Parent.Parent.Body.VehicleSeat
local maxspeed = seat.MaxSpeed

while true do
	task.wait(0.1)
	local Look = script.Parent.CFrame.LookVector * script.Parent.Speed.Value

	script.Parent.BV.Velocity = Vector3.new(Look.X * seat.Throttle,0,Look.Z * seat.Throttle)
end

If the throttle is only -1, 0, or 1, then you can multiply the Look variable by it.

A number multiplied by 1 stays the same.
A number multiplied by 0 is 0.
A number multiplied by -1 becomes negative.

So, you don’t need that nasty if statement there.

1 Like

How can I decrease the acceleration and also make it so that you can’t move backwards as fast?

Well, if you were using your original script, you would want to make it so that if the throttle was -1 then you set the velocity to

local Dampening = 0.5
script.Parent.BV.Velocity = Vector3.new(-look.X*Dampening, 0, -look.Z*Dampening)

Or something like that…

And how can I decrease the speed of acceleration, but keep the max speed the same?