What's the mobile/gamepad equivalent to player:GetMouse().Target

Hello, I’m wanting to create a tool in which fires a remote event to the server containing the information of a part. For mouse this is easy as all I need to do is get the player’s mouse and then the target of that. However, for mobile and for gamepad I’m not sure what to do for this. Any suggestions?

Here’s my code so far:

userInput.InputBegan:Connect(function(key)
	
	--FOR EQUIPPING/UNEQUIPPING CRUCIFIX
	if key.UserInputType == Enum.UserInputType.MouseButton3 or key.KeyCode == Enum.KeyCode.ButtonL3 or key.UserInputType == Enum.UserInputType.Touch then
		player.PlayerGui.Sounds.Click:Play()
		animationEquip()
		game.ReplicatedStorage.Events.CrucifixTool:FireServer()
		wait()
		changingText()
		
		
	--FOR USING THE CRUCIFIX TOOL
	elseif (key.UserInputType == Enum.UserInputType.MouseButton1 or key.KeyCode == Enum.KeyCode.ButtonA or key.UserInputType == Enum.UserInputType.Touch) and player.Character:FindFirstChild("Crucifix") then
		if userInput.MouseEnabled then
			game.ReplicatedStorage.Events.CrucifixTool:FireServer(player:GetMouse().Target) --Fires an event to the server which gives properties of the part
		elseif userInput.TouchEnabled then
			game.ReplicatedStorage.Events.CrucifixTool:FireServer(GET TOUCHED PART HERE)
		
		end
	
	end
end)

I’m pretty sure that :GetMouseLocation() can be used to determine where the virtual mouse is on mobile and console. To get the target though, you’ll have to use raycasting. I’ll give you an example!

local MouseLocation = game:GetService("UserInputService"):GetMouseLocation()
local Camera = workspace.Camera
local ViewRay = Camera:ViewportPointToRay(MouseLocation.X, MouseLocation.Y)

local Ray = workspace:Raycast(ViewRay.Origin, ViewRay.Direction * 100) -- 100 is just how far you want the ray to be casted

local Target = Ray.Instance

This should work! If you have any questions, let me know!

3 Likes

Great! Thank you for informing me of this. Have a great day!

1 Like

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