Need help with mobile controls

Hey! I’m currently working on a custom joystick for mobile players. It works perfectly on a pc, but I just don’t know the Touch alternatives to mouse events. InputChanged → TouchMoved.

Could anyone help me make this suitable for a phone user? Thanks.
My code:

function MobileControls()
	--// Setup
	local InputChangedConnections = {}
	local MaxPosX = script.Parent.AbsoluteSize.X * 0.4
	local MaxPosY = script.Parent.AbsoluteSize.Y * 0.4

	--// Code
	Circle.MouseButton1Down:Connect(function()
		print("Mouse down", UserInputService:GetMouseLocation())
		--// Save the mouse's current position
		local StarterMouseLocation = UserInputService:GetMouseLocation()
		--// Snap the circle to the mouse position
		Circle.Position = UDim2.new(0.5, StarterMouseLocation.X, 0.5, StarterMouseLocation.Y)

		--// Move the circle when the mouse moves
		local function onTouchMoved(input)
			--// Change the circle's position
			if input.UserInputType == Enum.UserInputType.Touch then
				local MouseLocation = UserInputService:GetMouseLocation()

				local direction = (MouseLocation - StarterMouseLocation).Unit
				local magnitude = (MouseLocation - StarterMouseLocation).Magnitude

				Circle.Position = UDim2.new(
					0.5,
					(direction * math.min(magnitude, MaxPosX)).X,
					0.5,
					(direction * math.min(magnitude, MaxPosY)).Y
				)

				--// Output
				OutputDirection = {
					X = (math.sign(direction.X) * math.min(magnitude, MaxPosX) / MaxPosX) * ((math.min(magnitude, MaxPosX) / MaxPosX) < THRESHOLD and 0 or 1) * ((math.abs(direction.X) < THRESHOLD) and 0 or 1),
					Z = (math.sign(direction.Y) * math.min(magnitude, MaxPosY) / MaxPosY) * ((math.min(magnitude, MaxPosY) / MaxPosY) < THRESHOLD and 0 or 1) * ((math.abs(direction.Y) < THRESHOLD) and 0 or 1)
				}

				local ArrowNumber = tostring(math.sign(OutputDirection.X))..","..tostring(math.sign(OutputDirection.Z))
				for i, v in pairs(Arrows:GetChildren()) do
					if v ~= DirectionToArrow[ArrowNumber] then
						v.BackgroundTransparency = 0.35
					else
						v.BackgroundTransparency = 0
					end
				end
			end
		end	

		table.insert(InputChangedConnections, 1, UserInputService.TouchMoved:Connect(onTouchMoved))
	end)

	UserInputService.TouchEnded:Connect(function(input)
		if input.UserInputType == Enum.UserInputType.Touch then
			print("Mouse up")
			--// Disconnect all connections
			for i, connection in pairs(InputChangedConnections) do
				connection:Disconnect()
			end
			table.clear(InputChangedConnections)
			--// Reset the circle's position
			Circle.Position = UDim2.new(0.5,0,0.5,0)
			--// Reset 'OutputDirection'
			OutputDirection = {
				X = 0,
				Z = 0
			}
			for i, v in pairs(Arrows:GetChildren()) do
				v.BackgroundTransparency = 0.35
			end
		end
	end)
end
1 Like

If anyone is on a phone, please join this game and tell me if the joystick works. (If you have time)

You can try with the phone emulator in studio too.

1 Like

How would I do that? I don’t know about anything like that lol :smiley:

Do you mean like this?

1 Like