Unexpected bounce when sliding after jump - Movement Script

When you press shift right before landing from a jump, the character bounces. I want it to just slide. It took me a long time and still can’t find the cause of it. Hope yall can help :smile_cat:

To replicate:

  1. In StarterPlayer set DevComputerMovementMode to Scriptable
  2. Script in StarterPlayerScripts
-- Services --
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

-- Variables --
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local KeyStates = {
	W = false;
	A = false;
	S = false;
	D = false;
}

local velocity = Vector3.new(0, 0, 0)
local vertVelocity = 0

local acceleration = 80
local deacceleration = 16
local groundVelocityCap = 16
local jumpHeight = 128
local gravity = -1

local isSliding = false
local isJumping = false

local moveDirection = Vector3.new(0, 0, 0)
local lastMoveDirection = Vector3.new(0, 0, 0)
local lastHumanoidRootPartLookVector

-- Input Handler --
UserInputService.InputBegan:Connect(function(input, _gameProcessed)
	if _gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.W then
		KeyStates.W = true
	elseif input.KeyCode == Enum.KeyCode.A then
		KeyStates.A = true
	elseif input.KeyCode == Enum.KeyCode.S then
		KeyStates.S = true
	elseif input.KeyCode == Enum.KeyCode.D then
		KeyStates.D = true
	elseif input.KeyCode == Enum.KeyCode.LeftShift then
		isSliding = true
	elseif input.KeyCode == Enum.KeyCode.Space then
		if not isJumping and Humanoid.FloorMaterial ~= Enum.Material.Air then
			isJumping = true
			vertVelocity = jumpHeight
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, _gameProcessed)
	if _gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.W then
		KeyStates.W = false
	elseif input.KeyCode == Enum.KeyCode.A then
		KeyStates.A = false
	elseif input.KeyCode == Enum.KeyCode.S then
		KeyStates.S = false
	elseif input.KeyCode == Enum.KeyCode.D then
		KeyStates.D = false
	elseif input.KeyCode == Enum.KeyCode.LeftShift then
		isSliding = false
	elseif input.KeyCode == Enum.KeyCode.Space then
		isJumping = false
	end
end)

-- RunService Loop --
RunService.Stepped:Connect(function(_currentTime, deltaTime)
	-- Wish direction
	local wishDir = Vector3.new(
		(KeyStates.D and 1 or 0) - (KeyStates.A and 1 or 0),
		0,
		(KeyStates.S and 1 or 0) - (KeyStates.W and 1 or 0)
	)

	if wishDir.Magnitude == 0 then
		wishDir = Vector3.new(0, 0, 0)
	end
	
	-- Acceleration and velocity cap on ground
	if wishDir.Magnitude > 0 and not isSliding then
		velocity = velocity + (wishDir * acceleration * deltaTime)
		if velocity.Magnitude > groundVelocityCap and Humanoid.FloorMaterial ~= Enum.Material.Air then
			velocity = velocity.Unit * groundVelocityCap
		end
	end

	-- Ground deacceleration
	if Humanoid.FloorMaterial ~= Enum.Material.Air and not isSliding and wishDir == Vector3.new(0, 0, 0) then
		if velocity.Magnitude < 0.1 then velocity = Vector3.new(0, 0, 0) end
		velocity = velocity * (1 - deacceleration * deltaTime)
	end
	
	if Humanoid.FloorMaterial ~= Enum.Material.Air then
		if isJumping then
			vertVelocity = jumpHeight
			isJumping = false
		else
			vertVelocity = 0
		end
	else
		vertVelocity = vertVelocity + (gravity * deltaTime)
	end

	-- Move Character and lock orientation when sliding
	if not isSliding then
		moveDirection = Vector3.new(velocity.X, vertVelocity, velocity.Z)
		
		lastMoveDirection = moveDirection
		lastHumanoidRootPartLookVector = HumanoidRootPart.CFrame.LookVector
	else
		moveDirection = lastMoveDirection 
		HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.CFrame.Position, HumanoidRootPart.CFrame.Position + lastHumanoidRootPartLookVector)
		task.delay(.5, function()
			isSliding = false
		end)
	end
	
	HumanoidRootPart.CFrame = HumanoidRootPart.CFrame * CFrame.new(moveDirection * deltaTime)
	LocalPlayer.PlayerGui.ScreenGui.Frame.TextLabel.Text = tostring(velocity.Magnitude)
end)

1 Like

Just from a quick overview, I wonder if maintaining the vertical velocity when sliding would change anything.

moveDirection = Vector3.new(lastMoveDirection.X, vertVelocity, lastMoveDirection.Z)
HumanoidRootPart.CFrame = CFrame.new(HumanoidRootPart.CFrame.Position, HumanoidRootPart.CFrame.Position + lastHumanoidRootPartLookVector)
task.delay(.5, function()
  isSliding = false
end)

Thanks alot! I overlooked that lastMoveDirection is not only setting X and Z but also Y.

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