How to accurately determine which direction an Xbox thumbstick is pointing?

Question is pretty clear. I have a rough understanding of what inputObject.Delta is, and in my first attempt of trying this, I just compared the X and Y values to zero to find the direction.

local leftThumbstickDir = nil

userinputServ.InputChanged:Connect(function(input, processed)
	if processed then return end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			if input.Delta.X > 0 then
				leftThumbstickDir = "Right"
			elseif input.Delta.X < 0 then
				leftThumbstickDir = "Left"
			elseif input.Delta.Y > 0 then
				leftThumbstickDir = "Up"
			elseif input.Delta.Y < 0 then
				leftThumbstickDir = "Down"
			end
		end
	end
end)

Though, I realized that the player would have to be incredibly precise for it to detect if the thumbstick was pointing up or down. I tried a few other things, but they didn’t work as intended.

I’d like to give the player some leeway so that if the thumbstick was pointing a little off angle then it wouldn’t matter. See image below…

cac66ea5467f08473e822461377d621d722b2589e

Please let me know if you have any methods to this. The solution might be very obvious, I am just tired at the moment.

simply move the x and y if statements away from each other.

userinputServ.InputChanged:Connect(function(input, processed)
	if processed then return end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			if input.Delta.X > 0 then
				print("Thumbstick pointing Right")
			elseif input.Delta.X < 0 then
				print("Thumbstick pointing Left")
			end

			if input.Delta.Y > 0 then
				print("Thumbstick pointing Up")
			elseif input.Delta.Y < 0 then
				print("Thumbstick pointing Down")
			end
		end
	end
end)

Ah, apologies. Maybe I should’ve made it clear that instead of print statements I’m actually changing a variable, since I refer to it in another function.

local leftThumbstickDir = nil

userinputServ.InputChanged:Connect(function(input, processed)
	if processed then return end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			if input.Delta.X > 0 then
				leftThumbstickDir = "Right"
			elseif input.Delta.X < 0 then
				leftThumbstickDir = "Left"
			end

			if input.Delta.Y > 0 then
				leftThumbstickDir = "Up"
			elseif input.Delta.Y < 0 then
				leftThumbstickDir = "Down"
			end
		end
	end
end)

If I did this, they would conflict with each other. Sorry for not making that clear.

Ah, If you’re doing this to handle movement, I would recommend being able to record both ways, if not, I believe checking if the delta is less than .5 and greater than -.5 would work.

local leftThumbstickDir = nil

userinputServ.InputChanged:Connect(function(input, processed)
	if processed then return end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.Thumbstick1 then
			if input.Delta.X > 0 and input.Delta.Y < 0.5 and input.Delta.Y > -0.5 then
				leftThumbstickDir = "Right"
			elseif input.Delta.X < 0 and input.Delta.Y < 0.5 and input.Delta.Y > -0.5  then
				leftThumbstickDir = "Left"
			elseif input.Delta.Y > 0 and input.Delta.X < 0.5 and input.Delta.X > -0.5  then
				leftThumbstickDir = "Up"
			elseif input.Delta.Y < 0 and input.Delta.X < 0.5 and input.Delta.X > -0.5 then
				leftThumbstickDir = "Down"
			end
		end
	end
end)
2 Likes