Help with easing character movement

Hi, I am trying to fork the ControlModule script so that I can ease character movement from 0 speed to the intended. I am not doing this with WalkSpeed as I believe that will interfere with other scripts.

This is what I have so far:

local easeTime = 0.5
local vectorMult = 0
local charMove = false

function ControlModule:OnRenderStepped(dt)
	if self.activeController and self.activeController.enabled and self.humanoid then
		-- Give the controller a chance to adjust its state
		self.activeController:OnRenderStepped(dt)

		-- Now retrieve info from the controller
		local moveVector = self.activeController:GetMoveVector()
		local cameraRelative = self.activeController:IsMoveVectorCameraRelative()

		local clickToMoveController = self:GetClickToMoveController()
		if self.activeController ~= clickToMoveController then
			if moveVector.magnitude > 0 then
				-- Clean up any developer started MoveTo path
				clickToMoveController:CleanupPath()
			else
				-- Get move vector for developer started MoveTo
				clickToMoveController:OnRenderStepped(dt)
				moveVector = clickToMoveController:GetMoveVector()
				cameraRelative = clickToMoveController:IsMoveVectorCameraRelative()
			end
		end

		-- Are we driving a vehicle ?
		local vehicleConsumedInput = false
		if self.vehicleController then
			moveVector, vehicleConsumedInput = self.vehicleController:Update(moveVector, cameraRelative, self.activeControlModule==Gamepad)
		end

		-- If not, move the player
		-- Verification of vehicleConsumedInput is commented out to preserve legacy behavior,
		-- in case some game relies on Humanoid.MoveDirection still being set while in a VehicleSeat
		--if not vehicleConsumedInput then
		if cameraRelative then
			moveVector = calculateRawMoveVector(self.humanoid, moveVector)
		end
		
		if moveVector > Vector3.new() then
			charMove = true
			task.spawn(function()
				while charMove == true do
					-- HELP NEEDED HERE
				end
			end)
		else
			charMove = false
			vectorMult = 0
		end
		self.moveFunction(Players.LocalPlayer, moveVector * vectorMult, false) -- Moves the player
		--end

		-- And make them jump if needed
		self.humanoid.Jump = self.activeController:GetIsJumping() or (self.touchJumpController and self.touchJumpController:GetIsJumping())
	end
end

Please pay attention to the part where it starts with if moveVector > Vector3.new then...
I want the script to gradually increase the vectorMult variable to 1 over a period of time set by easeTime as long as charMove is true, otherwise vectorMult will reset to 0.

You can take advantage of the dt time variable to interpolate your scalar without needing to generate a new thread. Try replacing this block…

if moveVector > Vector3.new() then
	charMove = true
	task.spawn(function()
		while charMove == true do
			-- HELP NEEDED HERE
		end
	end)
else
	charMove = false
	vectorMult = 0
end

…with this…

if moveVector.Magnitude > 0 then
	charMove = true
	vectorMult = math.min(vectorMult + dt / easeTime, 1)
else
	charMove = false
	vectorMult = 0
end

Hi,
I came up with a hacky solution before you sent this, I think my method is pretty similar but it uses a lerp function.

local easeSpeed = 0.06
local fpsDelta = 1/60
local vectorMult = 0

function Lerp(a,b,c)
	return a + (b - a) * c
end

function ControlModule:OnRenderStepped(dt)
	if self.activeController and self.activeController.enabled and self.humanoid then
		-- Give the controller a chance to adjust its state
		self.activeController:OnRenderStepped(dt)

		-- Now retrieve info from the controller
		local moveVector = self.activeController:GetMoveVector()
		local cameraRelative = self.activeController:IsMoveVectorCameraRelative()

		local clickToMoveController = self:GetClickToMoveController()
		if self.activeController ~= clickToMoveController then
			if moveVector.magnitude > 0 then
				-- Clean up any developer started MoveTo path
				clickToMoveController:CleanupPath()
			else
				-- Get move vector for developer started MoveTo
				clickToMoveController:OnRenderStepped(dt)
				moveVector = clickToMoveController:GetMoveVector()
				cameraRelative = clickToMoveController:IsMoveVectorCameraRelative()
			end
		end

		-- Are we driving a vehicle ?
		local vehicleConsumedInput = false
		if self.vehicleController then
			moveVector, vehicleConsumedInput = self.vehicleController:Update(moveVector, cameraRelative, self.activeControlModule==Gamepad)
		end

		-- If not, move the player
		-- Verification of vehicleConsumedInput is commented out to preserve legacy behavior,
		-- in case some game relies on Humanoid.MoveDirection still being set while in a VehicleSeat
		--if not vehicleConsumedInput then
		if cameraRelative then
			moveVector = calculateRawMoveVector(self.humanoid, moveVector)
		end
		
		if moveVector ~= Vector3.new() then
			vectorMult = Lerp(vectorMult, 1, easeSpeed * dt/fpsDelta)
		else
			vectorMult = 0
		end
		self.moveFunction(Players.LocalPlayer, moveVector * vectorMult, false) -- Moves the player
		--end

		-- And make them jump if needed
		self.humanoid.Jump = self.activeController:GetIsJumping() or (self.touchJumpController and self.touchJumpController:GetIsJumping())
	end
end
1 Like

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