Help with 3D Platformer Movement

Hey Devs,
I was working on a movement system for a 3D Platformer and I finished coding an acceleration script.
The code looks like the following:

local TS = game:GetService("TweenService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local originalspeed = 16
local maxspeed = 50
local speeduptime = 3

local speedup = TS:Create(hum, TweenInfo.new(speeduptime, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {WalkSpeed = maxspeed})

hum.Running:Connect(function(spd)
	if spd > originalspeed - 1 then
		speedup:Play()
	elseif spd <= 0 then
		speedup:Cancel()
		hum.WalkSpeed = originalspeed
	end
end)

It changes the Player’s speed using a Tween, it’s simple, but seems to work.
There’s a two issues I can’t find a solution for though:

  • If the Player turns backwards (they’re moving with W and suddenly switch to S) I wanted to slow them down. How do I go about doing this? Since the Player’s speed doesn’t decrease when turning backwards, I can’t find a way to check for when they turn backwards so I can decrease their speed myself.

  • When the Player releases the W key (stops moving) the Character just instantly stops. I wanted to make them gradually (but quickly) slow down, somewhat like how Sonic games do it, for example.

I’m not asking for code examples or anything, just please guide me towards a solution for these two problems.
Thanks in advance everyone, have a good day/night :slight_smile:

Heres an attempt to cook up a solution. However, you should confirm if you game is in first person (good) and/or if there are any modifications made that make the player move in a direction not conditional of the direction of the camera (bad)

Here’s the script, lmk if you need any part of how it works to be clarified:

local TS = game:GetService("TweenService")
local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")
local cam = game.Workspace.CurrentCamera

local originalspeed = 16
local maxspeed = 50
local speeduptime = 3
local slowdownTime = 1 -- change to whatever u want

local speedup = TS:Create(hum, TweenInfo.new(speeduptime, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {WalkSpeed = maxspeed})
local slowdown = TS:Create(hum, TweenInfo.new(slowdownTime, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, false, 0), {WalkSpeed = originalspeed})

local movingForward = false
local movingBackward = false

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		movingForward = true
	elseif input.KeyCode == Enum.KeyCode.S then
		movingBackward = true
	end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
		movingForward = false
	elseif input.KeyCode == Enum.KeyCode.S then
		movingBackward = false
	end
end)

RS.Heartbeat:Connect(function()
	if movingForward then
		local charDirection = Vector3.new(hrp.CFrame.lookVector.X, 0, hrp.CFrame.lookVector.Z)
		local camDirection = Vector3.new(cam.CFrame.lookVector.X, 0, cam.CFrame.lookVector.Z)
		local dotProd = charDirection:Dot(camDirection)
		
		if dotProd < 0 then 
			slowdown:Play()
			speedup:Cancel()
		else
			speedup:Play()
			slowdown:Cancel()
		end
	elseif movingBackward then
		slowdown:Play()
		speedup:Cancel()
	else
		slowdown:Play()
		speedup:Cancel()
	end
end)
1 Like

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