Help with gui sway script base on camera and character motion

Hi!, I have been working on a script that moves GUI base on camera and player movement and for some reason it only works if I walk straight or sideways and malfunction when I move diagonally. I do not have any video on it sry :bowing_man: but when walking diagonally, the GUI would not shift proportionally

This is the script that I have been using

local RS = game:GetService('RunService')
local UIS = game:GetService('UserInputService')
local TWS = game:GetService('TweenService')

local Player = game.Players.LocalPlayer
local Frame = script.Parent:WaitForChild('Offset')
local Position = Frame.Position
local Humanoid = Player.Character:WaitForChild('Humanoid')
local HumanoidRootPart = Player.Character:WaitForChild('HumanoidRootPart')
local Camera = workspace.Camera

local SwayX,SwayY = 0,0
local SwayX1,SwayY1,SwayZ1 = 0,0,0
local OriginalPosition = HumanoidRootPart.CFrame.Position
local SwaySpeed = 5
local Direction = Vector3.new(0,0,0)

local function Lerp (a,b,m)
	return a+(b-a)*m
end

RS.RenderStepped:Connect(function(dt)
	
	local Delta = UIS:GetMouseDelta()
	SwayX = Lerp(SwayX,Delta.X,dt*SwaySpeed)
	SwayY = Lerp(SwayY,Delta.Y,dt*SwaySpeed)
	
	local CurrentPosition = HumanoidRootPart.CFrame.Position
	
	if not (CurrentPosition == OriginalPosition) then
		if not (Direction == Humanoid.MoveDirection) then
			Direction =  Humanoid.MoveDirection
			
		else
			Direction = Vector3.new(0,0,0)
			
		end
		
	else	
		Direction = Vector3.new(0,0,0)
		
	end
	
	SwayX1 = Lerp(SwayX1,Direction.X*30,dt*SwaySpeed)
	SwayY1 = Lerp(SwayY1,Direction.Y*30,dt*SwaySpeed)
	SwayZ1 = Lerp(SwayZ1,Direction.Z,dt*SwaySpeed)

	local I = Humanoid.MoveDirection:Dot(Camera.CFrame.LookVector)
	if I > 0.7 or I < - 0.7 then
		SwayX1 = 0
	end
	
	
	local TargetPosition = Position + UDim2.fromOffset(SwayX + SwayX1,SwayY + SwayY1 )
	local tweenInfo = TweenInfo.new(0.1)
	local tween = TWS:Create(Frame,tweenInfo,{Position = TargetPosition})
	tween:Play()
	OriginalPosition = CurrentPosition
	
	
end)

Any help is much apprecitated :slight_smile: