Help making the player smoothly change direction in a zero gravity movement script

No idea how to start this soo

  1. What do you want to achieve? I am trying to make a zero gravity script smoothly change directions when in motion

  2. What is the issue? if I am moving forwards, and try to switch directions while in motion, it’s really stuttery, as seen below

  3. What solutions have you tried so far? various loops to slow the player down instead of just making them snap to the next velocity, none of which have worked, and I have tried looking for people with this same problem on the devforum to no avail

here is the part of the script that I am referring to

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
onsapce = false

local speed = 0
local AC = 0.5
--
local bg = Instance.new("BodyGyro",character:WaitForChild("HumanoidRootPart"))
bg.MaxTorque = Vector3.new()
bg.P = 100
bg.D = 30
local bv = Instance.new("BodyVelocity",character:WaitForChild("HumanoidRootPart"))
bv.MaxForce = Vector3.new()
bv.P = 0


rs.Heartbeat:Connect(function()
	if not character.Humanoid.Sit then
		bg.CFrame = mouse.Hit
	end
end)

Wdown = false
Sdown = false
Edown = false
Qdown = false
Ddown = false
Adown = false
uis.InputBegan:Connect(function(input,gameren)
	if not gameren and onsapce then
		local key = input.KeyCode
		if key == Enum.KeyCode.W then
			Wdown = true
			Sdown = false
			
			while Wdown and speed < 10 do
				speed = speed + AC
				bv.Velocity = character.HumanoidRootPart.CFrame.LookVector * speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = character.HumanoidRootPart.CFrame.LookVector * speed
		elseif key == Enum.KeyCode.S then
			Sdown = true
			Wdown = false
			
			while Sdown and speed < 10 do
				speed = speed + AC
				bv.Velocity = character.HumanoidRootPart.CFrame.LookVector * -speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = character.HumanoidRootPart.CFrame.LookVector * -speed
		elseif key == Enum.KeyCode.A then
			Adown = true
			Ddown = false
			while Adown and speed < 10 do
				speed = speed + AC
				bv.Velocity = character.HumanoidRootPart.CFrame.RightVector * -speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = character.HumanoidRootPart.CFrame.RightVector * -speed
		elseif key == Enum.KeyCode.D then
			Ddown = true
			Adown = false
			while Ddown and speed < 10 do
				speed = speed + AC
				bv.Velocity = character.HumanoidRootPart.CFrame.RightVector * speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = character.HumanoidRootPart.CFrame.RightVector * speed
		elseif key == Enum.KeyCode.Q then
			Qdown = true
			Edown = false
			while Qdown and speed < 10 do
				speed = speed + AC
				bv.Velocity = character.HumanoidRootPart.CFrame.UpVector * -speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = character.HumanoidRootPart.CFrame.UpVector * -speed
		elseif key == Enum.KeyCode.E then
			Edown = true
			Qdown = false
			while Edown and speed < 10 do
				speed = speed + AC
				bv.Velocity = character.HumanoidRootPart.CFrame.UpVector * speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = character.HumanoidRootPart.CFrame.UpVector * speed
		end
	end
end)

uis.InputEnded:Connect(function(input,gameren)
	if not gameren then
		local key = input.KeyCode
		if key == Enum.KeyCode.W and not Sdown then
			Wdown = false
			while speed > 0 do
				speed = speed - AC
				bv.Velocity = character.HumanoidRootPart.CFrame.LookVector * speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = Vector3.new()
		elseif key == Enum.KeyCode.S and not Wdown then
			Sdown = false
			while speed > 0 do
				speed = speed - AC
				bv.Velocity = character.HumanoidRootPart.CFrame.LookVector * -speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = Vector3.new()
		elseif key == Enum.KeyCode.A and not Ddown then
			Adown = false
			while speed > 0 do
				speed = speed - AC
				bv.Velocity = character.HumanoidRootPart.CFrame.RightVector * -speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = Vector3.new()
		elseif key == Enum.KeyCode.D and not Adown then
			Ddown = false
			while speed > 0 do
				speed = speed - AC
				bv.Velocity = character.HumanoidRootPart.CFrame.RightVector * speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = Vector3.new()
		elseif key == Enum.KeyCode.Q and not Edown then
			Qdown = false
			while speed > 0 do
				speed = speed - AC
				bv.Velocity = character.HumanoidRootPart.CFrame.UpVector * -speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = Vector3.new()
		elseif key == Enum.KeyCode.E and not Qdown then
			Edown = false
			while speed > 0 do
				speed = speed - AC
				bv.Velocity = character.HumanoidRootPart.CFrame.UpVector * speed
				wait(0.01)
			end
			wait(AC)
			bv.Velocity = Vector3.new()
		end
	end
end

hopefully all that is enough to Diagnose the Problem™

1 Like

It could be due to the amount of parts in the game.

not a lag issue