How to detect gamepad thumbstick movement, and if it's been moved little or all the way?

I’m setting up xbox controls on my game and I need to know how to detect if a thumbstick is being angled with someone’s thumb. I didn’t know how to set this up in a simple way so I looked at another developer’s code and pasted it into a local script to experiment with.

local UserInputService = game:GetService("UserInputService")

local function OnInputChanged(Input, GameProcessedEvent)
	while wait(10) do
		if Input.KeyCode == Enum.KeyCode.Thumbstick1 then
			--print("Thumbstick 1")
			if Input.Delta.X >= 0.1 then
				print("LEFTStickWentRight")
			elseif Input.Delta.X <= 0.1 then
				print("LEFTStickWentLeft")
			elseif Input.Delta.Y >=0.1 then
				print("LEFTStickWentUp")
			elseif Input.Delta.Y <= 0.1 then
				print("LEFTStickWentDown")
			end
		--elseif Input.KeyCode == Enum.KeyCode.Thumbstick2 then
			--print("Thumbstick 2")
			--if Input.Delta.X > 0 then
				--print("Right")
			--elseif Input.Delta.X < 0 then
				--print("Left")
			--end
		end
	end
end

UserInputService.InputChanged:Connect(OnInputChanged)

I doubt if I’m even doing the naming correctly, but this is what the output gave me.

It was really laggy when I opened it up. The left thumbstick was detected but not in the manner I wanted. I want the text “LEFTStickWentRight” to be printed if I move the thumbstick right and “LEFTStickWentLeft” if I move it left. Same thing up and down. But the inputs seemed like it didn’t matter in the output.

1 Like

You have a infinite while loop within an event?

1 Like

Thanks for pointing that out it’s besides the point

I know you probably don’t need help with this anymore, but for anyone else who is confused over the same issue, this is what you should do:
First of all, if you are trying to get the angle of the thumbstick, don’t use delta. Instead, use position. Position will return a number between -1 and 1 for the x and y axis. You can use those values to get the angle of the thumbstick.
For example, if Input.Position.X equals 1, then the thumbstick is right.

16 Likes