I’m trying to make a custom movement system for an FPS game I’m working on with my friends. When I hold W down and the player moves forward they’re being pushed down into the ground for some reason.
Here’s a video of that:
Here’s my code:
(MaxSpeed is 25)
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Humanoid = character:WaitForChild("Humanoid")
local maxSpeed = Humanoid:GetAttribute("MaxSpeed")
local wPressed = false
local currentSpeed = 0
local velocityMultiplier = 20
local velocity = Instance.new("LinearVelocity")
velocity.MaxForce = math.huge
velocity.RelativeTo = Enum.ActuatorRelativeTo.World
velocity.Attachment0 = character.HumanoidRootPart.RootAttachment
velocity.Parent = character.HumanoidRootPart
UserInputService.InputBegan:Connect(function(input, chat)
if chat then return end
if input.KeyCode == Enum.KeyCode.W then
wPressed = true
end
end)
UserInputService.InputEnded:Connect(function(input, chat)
if chat then return end
if input.KeyCode == Enum.KeyCode.W then
wPressed = false
end
end)
RunService.Stepped:Connect(function(tm, deltaTime)
if wPressed then
if velocity.VectorVelocity.Magnitude < maxSpeed then
velocity.VectorVelocity += character.HumanoidRootPart.CFrame.LookVector * velocityMultiplier * deltaTime
end
else
if velocity.VectorVelocity.Magnitude > 1 then
velocity.VectorVelocity -= character.HumanoidRootPart.CFrame.LookVector * velocityMultiplier * deltaTime
elseif velocity.VectorVelocity.Magnitude < 1 then
velocity.VectorVelocity = Vector3.new(0, 0, 0)
end
end
end)
Any way to solve this? Thanks in advance