Help pls about screen center cursor?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
  • i want to like have accurate raycast to a cursor in the middle of the screen for
    mobile players on third person

image_2023-11-15_083101149

  1. What is the issue?
  • i tried raycasting from the screen to get a position so the hitscan gun can shoot,
    but the ray isnt accurate to the cursor’s position
  1. What solutions have you tried so far?
  • i tried adding “camera.Cframe * CFrame.new” but it doesnt work and
    honestly i dont know any more way to “fix” this issue i have
2 Likes

Hi, you want to have the position of the frame like this ?:

Try this script:

--[[ Local Script ]]--
local RunService = game:GetService("RunService")

local Camera = game.Workspace.CurrentCamera
local Frame = script.Parent

local part = Instance.new("Part")
part.Parent = game.Workspace
part.Anchored = true
part.Size = Vector3.new(2, 2, 2)
part.Color = Color3.new(1, 0, 0)

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {part}

RunService.RenderStepped:Connect(function()
	local position = Frame.AbsolutePosition
	local size = Frame.AbsoluteSize
	local unitRay = Camera:ScreenPointToRay(position.X + size.X/2, position.Y + size.Y/2)
	
	local raycastResult = game.Workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, raycastParams)
	local frameWorldSpace = raycastResult and raycastResult.Position or unitRay.Origin + unitRay.Direction * 1000
	
	part.Position = frameWorldSpace
end)

image

If you want just have the position of the frame and not the part following, just delete “local part” and others! (sorry for mistakes, im french)

2 Likes

oh, sorry for the late “solution” thing i didnt actually think someone would give me one, ive scrolled around devforum and found 2 and i thought of doing it.

you, sir. are a life saver. thank you

1 Like

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