Need help trying to return a string direction depending on move direction from humanoid

Found this code on DevForum but it’s returning differently depending on my orientation in the world:

local Directions = {
	['Forward'] = Vector3.new(0, 0, -1),
	['Left'] = Vector3.new(-1, 0, 0),
	['Backwards'] = Vector3.new(0, 0, 1),
	['Right'] = Vector3.new(1, 0, 0)
}

local function WorldMovingDirection(dir, MultiDirectional)
	--//Made by Juni_Purr and ImPremiumJay
	local angle = math.atan2(dir.X, -dir.Z)--Gives Angle
	local quarterTurn =	math.pi/2

	local CurrectVector = nil
	local MovingDirection
	if dir == Vector3.new(0,0,0) then return 'Backwards' end--If they are standing still they are idle
	angle = -math.round(angle / quarterTurn) * quarterTurn

	local newX =math.round(-math.sin(angle))
	local newZ = math.round(-math.cos(angle))

	if math.abs(newX) <= 1e-10 then newX = 0 end
	if math.abs(newZ) <= 1e-10 then newZ = 0 end
	CurrectVector = Vector3.new(newX, 0, newZ)

	for Direction, Vector in Directions do
		-- Compare Directions
		if CurrectVector == Vector then--If players vector equals a vector in table Directions
			MovingDirection = Direction--Im not explaining this
		end

	end

	return MovingDirection
end

The code’s a bit wonky with directions. Instead of rounding off angles, try using dot product to compare movement and see which way you’re actually going.

1 Like