How can I make it so if i click a gui it clicks in the middle?

I am making a small little battle system, that requires clicking the opponent to do damage, but for mobile I want it different, there is a crosshair in the middle, and when you click a gui it clicks in the middle instead of just anywhere, I don’t know how to pull this off but if any of you can please tell me! I am not that good at scripting.

1 Like

Do you mean like snap whats being clicked on?

1 Like

im saying like is there a way to detect a click in the middle or atleast put like a part in front of my camera to detect a click in the middle for mobile players

Put an invisible GUI button in the middle of the screen?

1 Like

yes but like mouse.target, lets say i used that and i clicked in the middle, i can detect if i hit a part or something like that, but cant do that with just a gui

You can shoot a raycast along the camera direction when the button is pressed, then treat the result like a mouse.target

1 Like

il look into the raycast documentation, but if you can, can you make a sample since i dont know how to use raycasting yet

Raycast takes a start point, direction, and RaycastParams object. It gives you nil if the ray did not hit anything and a RaycastResult if it did. The length of the direction parameter is the max length of the ray checked, so you can get the direction vector pointing down the center of the screen with:
camera.CFrame * -Vector3.zAxis * maxRayLength

1 Like

so i can cast a ray from the head (since its first person) in the direction my heads facing, but how do i know when i have a raycastresult?

When the return value of Raycast(…) is not nil, it is a RaycastResult with an Instace and Position property (and others)

1 Like

i have been trying to make it work, but my ray only detects a baseplate, not a character that i wanted it to

script.Parent.MouseButton1Down:Connect(function()
	local pos = game.Players.LocalPlayer.Character.Head.Position
	local rot = game.Players.LocalPlayer.Character.Head.Orientation
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true
	local raycastResult = workspace:Raycast(pos, rot, raycastParams, 12)
	if raycastResult then
		print(raycastResult.Instance.Name)
		if raycastResult.Instance.Parent:FindFirstChild("Humanoid") then
			game.Players.LocalPlayer.Character:WaitForChild("Tool").Hit:FireServer(raycastResult.Instance.Parent)
		end
	else
		warn("No raycast result!")
	end
end)

The second argument to Raycast does not accept an Orientation, you need to give it a direction vector, such as:
workspace.CurrentCamera.CFrame * -Vector3.zAxis * 1000

1 Like

yeah, i got that part working, but now the raycast only detects if im right beside/touching the character

Put a part at pos + rot to see where it’s aiming.

1 Like

when i do that it spawns a part inside of me

Try this:

local currentCamera = workspace.CurrentCamera
local viewportSize = currentCamera.ViewportSize

local length = 500
local unitRay = currentCamera:ScreenPointToRay(viewportSize.X / 2, viewportSize.Y / 2)
local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * length)

You can include RaycastParams if you want.

1 Like