If you block the mouse it also counts for mobile users

My game is about shooting and I want the mobile players to aim with a crosshair that is in the center (Frame) but I read that in the case of mobile phones Mouse.Hit.Position cannot be used, and I want you to tell me how I can do so I fired the RAYO (bullet) and it shot straight according to the direction of the sight that is in the center, my game is with a blocked camera and with first persony.

1 Like

Is it possible to force an object in gui to follow the users finger? Because from there you could transfer the coordinates to the 3d game space

I don’t know what you mean by force, I want it to transfer the coordinates but as long as I follow the crosshairs in the middle, and the frame is in the middle of the screen, but I don’t know how to get those weird mathematical things

1 Like

I was hypothesizing that you could get a gui button or something to follow where the person is touching the screen on mobile. But I’m only now realizing the redundancy

Maybe try doing InputObject.UserInputType == Enum.UserInputType.Touch then get the position by doing Inputobject.Position

1 Like

waiting from a reply how solution

1 Like

This post was incredibly confusing to read, but what I understood what that you wanted to shoot a ray from the center of the user’s screen. Here is some code to do this:

local maxDistance = 1000 -- how far the bullet can shoot

local uis = game:GetService("UserInputService")

-- get the current camera
local camera = workspace.CurrentCamera

-- this function fires a ray from the center of your screen and creates a part wherever that ray lands.
-- it needs raycast parameters that blacklist your character (or whitelist hitboxes) so it doesn't hit the character.
local function bulletRay()
	local unit = camera:ViewportPointToRay(camera.ViewportSize.X/2,camera.ViewportSize.Y/2) -- get's a unit ray from the center of the screen.
	local ray = workspace:Raycast(unit.Origin,unit.Direction*maxDistance,myRaycastParams) -- put your raycast parameters here.
	print(ray)
	if ray == nil then return end
	Instance.new("Part",workspace).Position = ray.Position
end

uis.TouchTap:Connect(bulletRay)

Let me know if you have any questions!

1 Like

I just implemented this a few days ago myself in my game that I’m working on. What you do is that you have a touch button to fire. If the crosshair is set at the middle of the screen, you can get what the middle is and convert that from 2D coordinates to 3D coordinates.

-- Returns the 3D coordinates from the given 2D coordinates.
local function get3Dfrom2D(x, y)
	local maxDist = 10000
	local unitRay = game.Workspace.CurrentCamera:ViewportPointToRay(x, y)
	local raycastResult = game.Workspace:Raycast(unitRay.Origin, unitRay.Direction * maxDist, raycastParams)
	local hit
	if raycastResult == nil then
		hit = Vector3.new(
			unitRay.Origin.X + unitRay.Direction.X * maxDist,
			unitRay.Origin.Y + unitRay.Direction.Y * maxDist,
			unitRay.Origin.Z + unitRay.Direction.Z * maxDist
		)
	else
		hit = raycastResult.Position
	end
	return hit
end

This function does the magic of converting a 2D position into a 3D position. The following code uses it to send a fire command to the server which the server checks for validity.

		local viewportSize = game.Workspace.CurrentCamera.ViewportSize
		local Xc = viewportSize.X / 2
		local Yc = viewportSize.Y / 2
		local rayhit = get3Dfrom2D(Xc, Yc)
		toolData.events.click:FireServer(rayhit)

Don’t worry about toolData as that’s part of my weapon data. But basically you send the 3D coordinates to the server for processing.

Thank you very much for your help, I am sure that you will have success with your knowledge, I am not giving you the solution because the other person helped me in MD. I really appreciate your help I hope you are well

1 Like

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