My Flying script is shaking errr

So, I made a little flying script, but for some reason, it’s very shaky… Little help

local player = game.Players.LocalPlayer
local char = player.Character
local velocity = Instance.new("BodyVelocity", char.HumanoidRootPart)
velocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
local UIS = game:GetService("UserInputService")
local xDelta = 0
local zDelta = 0
local scale = 30
--[[local idle = Instance.new("Animation")
idle.AnimationId = "rbxassetid://2083818553" 
local idleTrack = char.Humanoid:LoadAnimation(idle)
idleTrack:Play()

local fly = Instance.new("Animation")
fly.AnimationId = "rbxassetid://2083775002" 
local flyTrack = char.Humanoid:LoadAnimation(fly)--]]

local IdleAndWalk = false
UIS.InputBegan:connect(function(input)
	local i = input.KeyCode 
	if i == Enum.KeyCode.D then
		xDelta = 5
	elseif i == Enum.KeyCode.A then
		xDelta = -5
	elseif i == Enum.KeyCode.W then
		zDelta = -5
	end
end)
UIS.InputEnded:connect(function(input)
	local i = input.KeyCode
	if i == Enum.KeyCode.D then
		if xDelta == 5 then
			xDelta = 0
		end
	elseif i == Enum.KeyCode.A then
		if xDelta == -5 then
			xDelta = 0
		end
	elseif i == Enum.KeyCode.W then
		if zDelta == -5 then
			zDelta = 0
		end
	end
end)

game:GetService("RunService").RenderStepped:connect(function()
	local cam = game.Workspace.CurrentCamera
	local CameraDirection = char.HumanoidRootPart.CFrame:toObjectSpace(cam.CFrame).lookVector.unit
	char.HumanoidRootPart.CFrame = CFrame.new(char.HumanoidRootPart.Position)
		* CFrame.Angles(math.asin(CameraDirection.y), 0 ,0)
	print(char.HumanoidRootPart.CFrame.lookVector)
	local c
	if xDelta ~= 0 or zDelta ~= 0 then
		--flyTrack:Play()
		if zDelta ~= 0 then
			if zDelta == 5 then
			 	cf = CFrame.new(char.HumanoidRootPart.CFrame.p, char.HumanoidRootPart.CFrame*CFrame.new(0, 0, (-zDelta*scale))*CFrame.Angles(0, math.rad(xDelta*scale), 0).p)
				velocity.Velocity = cf.lookVector*scale
			else
				cf = CFrame.new(char.HumanoidRootPart.CFrame.p, char.HumanoidRootPart.CFrame*CFrame.new(0, 0, (zDelta*scale))*CFrame.Angles(0, math.rad(xDelta*scale), 0).p)
				velocity.Velocity = cf.lookVector*scale
			end
		else
			cf = CFrame.new(char.HumanoidRootPart.CFrame.p, char.HumanoidRootPart.CFrame*CFrame.new(0, 0, -5)*CFrame.Angles(0, math.rad(xDelta*scale), 0).p)
			velocity.Velocity = cf.lookVector*scale
		end
	else
		--flyTrack:Stop()
	end
	if xDelta == 0 and zDelta == 0 then
		velocity.Velocity = Vector3.new(0, 0, 0)
	end
	print(xDelta)
	print(zDelta)
end)
2 Likes

The biggest issue with your script lies in the RenderStepped event.
I spent an hour or so tinkering around with it and got this. It’s a bit rigid but I believe it will do what you want.

--// Services
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

--// Service Objects
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local CurrentCamera = workspace.CurrentCamera

--// Objects
local BodyVelocity = Instance.new("BodyVelocity")
BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyVelocity.Velocity = Vector3.new(0,0,0)
local BodyGyro = Instance.new("BodyGyro")
BodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
BodyGyro.D = 100
BodyGyro.P = 20000
BodyGyro.CFrame = HumanoidRootPart.CFrame
local xDelta = 0
local zDelta = 0
local Speed = 30 -- Studs/Second

BodyVelocity.Parent = HumanoidRootPart
BodyGyro.Parent = HumanoidRootPart

UserInputService.InputBegan:Connect(function(InputObject)
	local KeyCode = InputObject.KeyCode
	if KeyCode == Enum.KeyCode.W then
		zDelta = zDelta - 1
	elseif KeyCode == Enum.KeyCode.A then
		xDelta = xDelta - 1
	elseif KeyCode == Enum.KeyCode.S then
		zDelta = zDelta + 1
	elseif KeyCode == Enum.KeyCode.D then
		xDelta = xDelta + 1
	end
end)

UserInputService.InputEnded:Connect(function(InputObject)
	local KeyCode = InputObject.KeyCode
	if KeyCode == Enum.KeyCode.W then
		zDelta = zDelta + 1
	elseif KeyCode == Enum.KeyCode.A then
		xDelta = xDelta + 1
	elseif KeyCode == Enum.KeyCode.S then
		zDelta = zDelta - 1
	elseif KeyCode == Enum.KeyCode.D then
		xDelta = xDelta - 1
	end
end)

RunService.RenderStepped:Connect(function()
	BodyGyro.CFrame = CurrentCamera.CFrame
	if xDelta ~= 0 or zDelta ~= 0 then
		local MoveDir = CFrame.new(CurrentCamera.CFrame.p, (CurrentCamera.CFrame * CFrame.new(xDelta, 0, zDelta)).p)
		BodyVelocity.Velocity = MoveDir.lookVector * Speed
	else
		BodyVelocity.Velocity = Vector3.new(0,0,0)
	end
end)
4 Likes