Touch problem with mobile

Hey there, I have a little problem regarding the mobile controls. Basically I have a function that repeats to turn the character with a bodygyro towards the mouse’s X and Z position and this works perfectly fine on pc, but when used on mobile it works until you move. Since you touch your screen a second time to move your joystick, it follows the second touch’s position instead of the first one. Is there a way to ignore this 2nd touch?

https://gyazo.com/34e42a6b1f62bee6ad87a983d1c84882

Maybe this could help you?

1 Like

It’s not really the joystick, it’s just the second touch on the screen in general that I want ignored.

There is a way to record specific touches and to track them using InputUserService (InputBegan is one of the events you can use, but I believe tracking would require a bit more code to work with the other input events (InputChanged, InputEnded, etc)). These events return input objects with information about the input but I don’t have much experience dealing with multitouch stuff, though hopefully this provides some extra info

1 Like

Yeah, but the problem is that it seems to always track the most recent touch and I’m afraid there is no way to track the first one without getting cancelled by the second touch.

Check out the InputObject docs, it goes over some ways to track individual touches

Sorry to revive this topic I discover the CameraInput script that ignore thumbstick on the 2nd input

--!nonstrict

local UserGameSettings: UserGameSettings = UserSettings():GetService("UserGameSettings")
local UserInputService = game:GetService("UserInputService")

local GuiService = game:GetService("GuiService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local playerGui: PlayerGui = player:WaitForChild("PlayerGui")
local dynamicThumbstickInput: InputObject?

local function isInDynamicThumbstickArea(pos: Vector3): boolean
	local touchGui = playerGui:FindFirstChild("TouchGui") :: ScreenGui
	local touchFrame: Frame = touchGui and touchGui:FindFirstChild("TouchControlFrame")
	local touchMovementMode = UserGameSettings.TouchMovementMode
	local thumbstickFrame: Frame?

	if touchFrame then
		if touchMovementMode == Enum.TouchMovementMode.Default or touchMovementMode == Enum.TouchMovementMode.DynamicThumbstick then
			thumbstickFrame = (touchFrame:FindFirstChild("DynamicThumbstickFrame") :: Frame)
		elseif touchMovementMode == Enum.TouchMovementMode.Thumbstick then
			thumbstickFrame = (touchFrame:FindFirstChild("ThumbstickFrame") :: Frame)
		end
	end

	if thumbstickFrame then
		local posTopLeft = thumbstickFrame.AbsolutePosition
		local posBottomRight = posTopLeft + thumbstickFrame.AbsoluteSize

		if
			pos.X >= posTopLeft.X 
			and pos.Y >= posTopLeft.Y 
			and pos.X <= posBottomRight.X 
			and pos.Y <= posBottomRight.Y
		then
			return true
		end
	end

	return false
end

UserInputService.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if input.UserInputState == Enum.UserInputState.Begin and input.UserInputType == Enum.UserInputType.Touch then
		if dynamicThumbstickInput == nil and isInDynamicThumbstickArea(input.Position) then
			dynamicThumbstickInput = input
			return
		end
		-- Do something here
	end
end)

UserInputService.InputChanged:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if input.UserInputState == Enum.UserInputState.Change and input.UserInputType == Enum.UserInputType.Touch then
		if input == dynamicThumbstickInput then
			return
		end
		-- Do something here
	end
end)

UserInputService.InputEnded:Connect(function(input: InputObject, gameProcessedEvent: boolean)
	if input.UserInputState == Enum.UserInputState.End and input.UserInputType == Enum.UserInputType.Touch then
		if input == dynamicThumbstickInput then
			dynamicThumbstickInput = nil
		end
		-- Do something here
	end
end)

GuiService.MenuOpened:Connect(function()
	if dynamicThumbstickInput then
		dynamicThumbstickInput = nil
	end
end)
3 Likes