So I’m trying to achieve source like movement for my game, and it has acceleration and deceleration.
However I noticed that if you press a movement button (WASD) while spawning, you start to constantly move in the opposite direction until you reset
the code is a modified version of This Code by boatbomber using Quenty’s Spring module, here it is:
local Player = game.Players.LocalPlayer
local Character = game.Players.LocalPlayer.Character
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Spring = require(script:WaitForChild("SpringModule"))
local v3 = Vector3.new
local Direction = Spring.new(v3())
Direction.Speed = 8
local Front = v3(0,0,1)
local Side = v3(1,0,0)
UserInputService.InputBegan:Connect(function(Input,GameProcessedEvent)
if not GameProcessedEvent and Input.UserInputType == Enum.UserInputType.Keyboard then
--Handle movement
if Input.KeyCode == Enum.KeyCode.W then
Direction.Target = Direction.Target - Front
elseif Input.KeyCode == Enum.KeyCode.S then
Direction.Target = Direction.Target + Front
elseif Input.KeyCode == Enum.KeyCode.A then
Direction.Target = Direction.Target - Side
elseif Input.KeyCode == Enum.KeyCode.D then
Direction.Target = Direction.Target + Side
end
end
end)
UserInputService.InputEnded:Connect(function(Input,GameProcessedEvent)
if not GameProcessedEvent and Input.UserInputType == Enum.UserInputType.Keyboard then
--Handle movement
if Input.KeyCode == Enum.KeyCode.W then
Direction.Target = Direction.Target + Front
elseif Input.KeyCode == Enum.KeyCode.S then
Direction.Target = Direction.Target - Front
elseif Input.KeyCode == Enum.KeyCode.A then
Direction.Target = Direction.Target + Side
elseif Input.KeyCode == Enum.KeyCode.D then
Direction.Target = Direction.Target - Side
end
end
end)
RunService:BindToRenderStep("Walking", 100, function()
Player:Move(Direction.Position, true)
end)
Any help would be greatly appreciated.