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.

2 Likes

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

1 Like

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

2 Likes

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

1 Like

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

2 Likes

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

1 Like

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

2 Likes

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?

1 Like

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

2 Likes

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)
1 Like

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

2 Likes

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

1 Like

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

2 Likes

when i do that it spawns a part inside of me

1 Like

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