Hello there, I recently scripted a movement system that works really well, and I want to make it so that when you move only forward, your speed increases and when you move any other direction, your speed will decrease back to normal.
I tried a method from another discussion, but it only uses the X, Y, and Z axis in the world and not the player. I want to make it so that it is relative to the player.
This is what I’ve tried but didn’t work.
if math.round(humanoid.MoveDirection.X) == 1 then
humanoid.WalkSpeed -= speedPerFrame * 5
end
if math.round(humanoid.MoveDirection.X) == -1 then
humanoid.WalkSpeed -= speedPerFrame * 5
end
if math.round(humanoid.MoveDirection.Z) == 1 then
humanoid.WalkSpeed -= speedPerFrame * 5
end
if math.round(humanoid.MoveDirection.Z) == -1 then
humanoid.WalkSpeed += speedPerFrame
end
Any suggestions would be helpful, also, I’m still learning in scripting so explaining code would also be helpful.
1 Like
Ok, after a lot of research I made my own detection system that finally works the way I want it too. Here’s what I made in case anybody is looking forward into using it.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Camera = game.Workspace.CurrentCamera
local RunService = game:GetService("RunService")
RunService.RenderStepped:Connect(function()
-- Left and right checks using RightVector of camera
if (humanoid.MoveDirection:Dot(Camera.CFrame.RightVector)) > 0.75 then
print("Player is moving right!")
end
if (humanoid.MoveDirection:Dot(Camera.CFrame.RightVector)) < -0.75 then
print("Player is moving left!")
end
-- You have to round for Forward and backwards using LookVector of camera instead
if (math.round(humanoid.MoveDirection:Dot(Camera.CFrame.LookVector))) > 0 then
print("Player is moving forward!")
end
if (math.round(humanoid.MoveDirection:Dot(Camera.CFrame.LookVector))) < 0 then
print("Player is moving backwards!")
end
end)
inside the “if” statements, I made it so that if you lose speed moving in that direction and you only gain speed going forward. This script also works in first person if you are wondering.
3 Likes