How to make player slowly deaccelerate when stop pressing move keys

I want to make player accelerate and deaccelerate while walking or idling.

The problem is I want to make player deaccelerate. As the movement keys aren’t being pressed. It completely idle your character.

Here’s the LocalScript inside StarterCharacterScript:

local players = game:GetService("Players").LocalPlayer
local character = players.Character
local humanoid = character:FindFirstChildOfClass("Humanoid")

local normalwalkspeed = 16
local notwalkspeed = 0

local userinputservice = game:GetService("UserInputService")
local tweenservice = game:GetService("TweenService")
local walk_accel_info = TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local runningtween = tweenservice:Create(humanoid, walk_accel_info, {WalkSpeed = normalwalkspeed})
local notrunningtween = tweenservice:Create(humanoid, walk_accel_info, {WalkSpeed = notwalkspeed})
local move = false


while task.wait() do
	if humanoid.MoveDirection.Magnitude > 0 then
		runningtween:Play()
		
	elseif humanoid.MoveDirection.Magnitude == 0 then
		notrunningtween:Play()
	
	end
	
end



I know few reasons that how it happens.

  1. Humanoid WalkSpeed is just when you press the movement keys and allows your character to move.

  2. Player stop moving immediately, because the MoveDirection.Magnitude is set to 0 when player stop pressing movement keys and make player stop immediately. The humanoid walkspeed tween still playing.

Can anyone help.

1 Like

You can do it like this

2 Likes

Consider using Momentum Controls 2:

I made it for cases like these where you’d like to add acceleration and deceleration to the player’s movement without needing to use physics constraints. You can control how strong the effect is by changing the Rate attribute’s value. The Rate’s unit is in seconds, so for example setting it to 4 would result in the player taking 4 seconds to reach their walkspeed :slight_smile::+1:

1 Like

Can I have an explaination of how it works?

I’m unsure who your reply is intended for, but I’ll explain how Momentum Controls 2 works. Essentially it uses the delta time in-order to smoothly interpolate between the input values, and clamps them to keep them within the correct range. The delta is divided by the rate to change the speed of the interpolation, which makes the acceleration/deceleration effect stronger or weaker depending on whether the rate is a large value (which would make acceleration/deceleration slower) or a small value (which would make the acceleration/deceleration faster). All it needs to start working is for it to be placed in StarterPlayerScripts, and it’s important that you don’t change the name of the script as otherwise the default character controller would still be loaded, and it will cause undesirable behavior as there will be 2 different scripts attempting to tell the character what to do. The video on the post shows an example on how to change the Rate’s value, which I made into an attribute in-order to make it possible to change it using a separate server script while the game is running, if you prefer

1 Like