Need help creating a 4 directional combat system

The issue I’m facing is trying to create 4 direcitonal combat system, meaning your swing direciton is dependant on where your camera is orientated, I tried multiple things like:


local StartingDirection = "Down"

local Beats = 0
local tempx,tempy,tempz = Camera.CFrame:ToOrientation()
local prevX = tempx
local prevY = tempy

RunService.RenderStepped:Connect(function()
	local X,Y,Z = Camera.CFrame:ToOrientation()
	if prevX ~= X and math.abs(prevX-X) >= .02 then
		local currentDirection
		
		if prevX < X  then
			currentDirection = "Down"
			
		elseif prevX > X then
			currentDirection = "Up"
		
		end 
		
		if currentDirection ~= StartingDirection  then
			if Beats then Beats += 1 else Beats = 1 end
			if Beats > 2  then
				Beats = 0
				print(StartingDirection)
				StartingDirection = currentDirection
			end
		end
	end
	
	if prevY ~= Y and math.abs(prevY-Y) >= .02 then
		local currentDirection

		if prevY < Y  then
			currentDirection = "Left"

		elseif prevY > Y then
			currentDirection = "Right"

		end 

		if currentDirection ~= StartingDirection  then
			if Beats then Beats += 1 else Beats = 1 end
			if Beats > 2  then
				Beats = 0
				print(StartingDirection)
				StartingDirection = currentDirection
			end
		end
	end
	
	prevX = X
end)

but if you start spinning around it triggers up and down, when it should be if you spin right it shouldn’t change direction, also if you start sliding your camera up it starts bugging like crazy.

External Media
1 Like
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player: Player = Players.LocalPlayer
local camera: Camera = workspace.CurrentCamera

RunService.RenderStepped:Connect(function()
	local character: Model = player.Character
	if not character then
		return
	end

	local humanoidRootPart: BasePart = character:FindFirstChild("HumanoidRootPart")
	if not humanoidRootPart then
		return
	end

	local directionFacing: Vector3 = (camera.CFrame.LookVector)
	local dirString = "Down"

	if math.abs(directionFacing.X) > math.abs(directionFacing.Y) then
		if directionFacing.Unit.X > 0 then
			dirString = "Right"
		else
			dirString = "LEft"
		end
		
	elseif math.abs(directionFacing.X) < math.abs(directionFacing.X)  then
		if directionFacing.Unit.Y > 0 then
			dirString = "Up"
		else
			dirString = "Down"
		end			
	end	

	print(dirString)
	
end)

I tried doing something like this, to determine which direction the camera is furthest from to select which way the sword will swing, but it’s still not working well, anyone got any ideas?