Is there a mouse.hit compatible with pc and mobile also ignoring uis

Is there a way to get a mouse script that is compatible with mobile and pc.

If yes, does it ignore UI elements?

to what i know mouse.hit works both on pc and mobile, and yes it does ignore ui because it gets the position of the mouse in 3d space (cframe)

strange when I click in a UI the mouse.hit updates

1 Like

Mouse.Hit is not an event function. It is a property of the mouse and it contains the mouse’s position in the 3d space. Mouse.Hit will ignore UI instances.

1 Like

for me it does not ignore uis :sob::sob::sob::sob::sob:. When i pass the mouse over a frame or a button it gives me a 3d position

1 Like

Yes. It ignores the UI. It gives you a position of the 3D space. UIs are 2D and are not in workspace, therefore the mouse ignores it. You are confusing yourself.

2 Likes

SORRY i ment it to NOT ignore the uis

You cannot do that. Mouse.Hit always will ignore the UIs because it’s not in a 3D space. If you are trying to see where the Mouse position is on the screen, use Mouse.X or Mouse.ViewSizeX.

here is a function from a cross-platform input module that i use
it’s compatible with pc, mobile, and controllers

the only catch is that for mobile, u need to provide the touch position that’s given by:
UIS.InputBegan/Ended/Changed

the function returns 2 vectors:
1st: 3d position of the mouse
2nd: surface normal if the mouse is hovering over a valid object

local UIS							= game:GetService("UserInputService")
local GuiService					= game:GetService("GuiService")

local Inset							= GuiService:GetGuiInset()

local BlacklistParams				= RaycastParams.new()
BlacklistParams.FilterType			= Enum.RaycastFilterType.Blacklist
BlacklistParams.IgnoreWater			= true

local function RayBlacklist(Origin: Vector3, Direction: Vector3, Blacklist: {Instance}): RaycastResult
	BlacklistParams.FilterDescendantsInstances = Blacklist
	return workspace:Raycast(Origin, Direction, BlacklistParams)
end

local Inputs = {}

function Inputs.GetMousePosition(Blacklist: table, Position: Vector2?): (Vector3, Vector3)
	local ScreenPosition = Position and (Position + Inset) or UIS:GetMouseLocation()
	local UnitRay = workspace.CurrentCamera:ScreenPointToRay(ScreenPosition.X, ScreenPosition.Y - Inset.Y)
	local Result = RayBlacklist(UnitRay.Origin, UnitRay.Direction * 0x2710, Blacklist)

	if Result then
		return Result.Position, Result.Normal
	end
	return (UnitRay.Origin + (UnitRay.Direction * 0x2710)), Vector3.yAxis
end

return Inputs

usage:

this will print the “mouse” position when the player presses: Mouse Button1, Controller L2, or taps on their screen

image

local UIS							= game:GetService("UserInputService")

local Blacklist						= {}
local Enums							= {
	[Enum.UserInputType.MouseButton1]	= true;
	[Enum.KeyCode.ButtonL2]				= true;
	[Enum.UserInputType.Touch]			= true;
};

local Inputs = require(script.Inputs)

local function Check(I: InputObject): boolean
	return (Enums[I.KeyCode] or Enums[I.UserInputType]) ~= nil
end

UIS.InputBegan:Connect(function(InputObject: InputObject, SunkInput: boolean)
	if SunkInput then return end
	if not Check(InputObject) then return end

	local MousePos = Vector3.zero -- default position to 0, 0, 0
	if InputObject.UserInputType == Enum.UserInputType.Touch then
		local InputPos = InputObject.Position
		MousePos = Inputs.GetMousePosition(Blacklist, Vector2.new(InputPos.X, InputPos.Y))
	else
		MousePos = Inputs.GetMousePosition(Blacklist)
	end

	print(MousePos)
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.