How would I add deceleration to a player's speed?

Hello everyone!
I’ve recently been looking to create a deceleration and acceleration system to player movement where similar to a car, going forward speeds the player over time and stopping movement from an input slows the player to a stop. (Ex. holding W speeds up over time, the player stops pressing W and they slow to a stop.)

I’ve tackled the acceleration part to this idea, but I’ve found that deceleration isn’t as easy to implement, would anyone know of a good way to do this?

4 Likes

You can instead increase a number value over time, then just subtract that continuously increasing number from the walkspeed to create deceleration.

1 Like

A very similar topic already exists.
Add Acceleration to the Player’s Speed?

1 Like

Thanks! Though looking through it the topic lacked the answer to my arching question of how to get deceleration working so I’ll have to find the answer elsewhere.

1 Like

So by deceleration, you mean that when the player stops giving input to move, his/her character will have a transition to a stop? I believe this can be done with a BodyVelocity with a max force of somewhere near Vector3.new(2500, 0, 2500).
I’d suggest having a script that’d create that body velocity, parent it to the humanoid root part, and then run through a loop and edit the humanoid root part’s velocity to match the one of the body velocity.

5 Likes

Yes, transitioning from the walk to a complete stop, right now if you are to stop giving input the character completely stops, I’d like to get it to where the character will instead slow to a stop.

Hi, I was reading an old thread of yours, to decelerate a player and have them slowly stop moving, or skid… Did you figure how how to do that , and if so how?

What are you using to accelerate the player. There are many ways to decelerate them, depending on how you accelerate them.

Hi,
Thanks for replying, I have not started any code, but was looking at this for acceleration.

If you have any suggestions for both , let me know and I will test it it.
Thanks!

Try this, it’s both acceleration and deceleration. (Edit: there are better ways to go about this, but a lot of people seem happy with the result this provides so I am going to leave it here.)

local Plr = game.Players.LocalPlayer
local Char = Plr.Character
local Hum = Char.Humanoid
local HRP = Char.HumanoidRootPart
local DefaultSpeed = game.StarterPlayer.CharacterWalkSpeed
local TS = game:GetService("TweenService")
local AccelerationTime = 0.4
local DV = nil
local DecelTween = nil
local DecelTween2 = nil
local AccelTween = TS:Create(Hum, TweenInfo.new(AccelerationTime, Enum.EasingStyle.Quad, Enum.EasingDirection.In), {WalkSpeed = DefaultSpeed})
local IsMoving = false

Hum.WalkSpeed = 10

Hum:GetPropertyChangedSignal("MoveDirection"):Connect(function() 
	if Hum.MoveDirection.Magnitude > 0 and IsMoving == false then
		if DecelTween and Hum.WalkSpeed ~= 0 then
			local WS = Hum.WalkSpeed
			DecelTween:Cancel()
			Hum.WalkSpeed = math.floor(WS)
			DecelTween2:Cancel()
			DV:Destroy()
			DV = nil
			DecelTween = nil
			DecelTween2 = nil
		elseif Hum.WalkSpeed == 0 then
			Hum.WalkSpeed = 10
		end
		IsMoving = true
		AccelTween:Play()
	elseif Hum.MoveDirection.Magnitude == 0 and IsMoving == true then
		if AccelTween.PlaybackState == Enum.PlaybackState.Playing then
			AccelTween:Cancel()
		end
		IsMoving = false
		DV = Instance.new("BodyVelocity")
		DV.MaxForce = Vector3.new(100000,0,100000)
		DV.Velocity = HRP.Velocity
		DV.Parent = HRP
		DecelTween = TS:Create(Hum, TweenInfo.new(AccelerationTime, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {WalkSpeed = 0})
		DecelTween2 = TS:Create(DV, TweenInfo.new(AccelerationTime, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Velocity = Vector3.new(0,0,0)})
		DecelTween:Play()
		DecelTween2:Play()
		delay(AccelerationTime, function()
			if DV then
				Hum.WalkSpeed = 10
				DV:Destroy()
			end
		end)
	end
end)

I just realized I accidentally bumped a 2 year old post…

10 Likes

cool thanks! I will check it out…

1 Like

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