I am currently working on a zombie survival game with mobile support and i wanted to make so the raycast weapons on mobile have a crosshair system similar to the XBOX’s default crosshair, its basically a crosshair but its vertical position is higher so the player’s character doesn’t obfuscate the crosshair.
Making a crosshair thats exactly at the middle of the screen is not that hard, the issue is that making that works even when its vertical position is higher than the player’s character.
What i specifically need is the crosshair’s position, just like “Mouse.Hit.p”.
This is what i’ve tried so far, but it doesn’t give the exact position i need:
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local camera = game.Workspace.CurrentCamera
local Rootpart = char:WaitForChild("HumanoidRootPart")
local bulletpos = Vector3.new((camera.CFrame.p.X-((camera.CFrame.p.X-Rootpart.CFrame.p.X)*2)),((camera.CFrame.p.Y-((camera.CFrame.p.Y-Rootpart.CFrame.p.Y-3)*2))),(camera.CFrame.p.Z-((camera.CFrame.p.Z-Rootpart.CFrame.p.Z)*2)))
You can use a few Camera methods to get the world position that is hovering under your Crosshair.
local cam = workspace.CurrentCamera
local inset = game.GuiService:GetGuiInset().Y
-- you might need to add or subtract the inset depending on which function you use below (idk which e)
local pos = crosshair.AbsolutePosition + crosshair.AbsoluteSize / 2
local ray = cam:ScreenPointToRay(pos.X, pos.Y) -- use if IgnoreGuiInset is disabled in your UI
local ray = cam:ViewportPointToRay(pos.X, pos.Y) -- use if IgnoreGuiInset is enabled in your UI
Hopefully this somewhat works because I can’t test it right now!
also, i first used “ViewportPointToRay()” because the “IgnoreGuInset” was set to true and it messed up with the ray direction, but then i changed it to “ScreenPointToRay()” and even with “IgnoreGuiInset” set to true, it worked perfectly