How to raycast the middle of the camera?

Hello so my issue is really simple. How do I raycast the middle of the camera?

I have a game that is in first personn and I want prompts to show up based on which prompt is closer to the very center of the camera.

I will put a dot in the middle of the users screen so that they can tell where the center is.

I would do this with the mouse (which I actually have right now) however mobile players don’t have a mouse so I cant.

here is my current code:

local Raycastparams = RaycastParams.new()
	Raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
	Raycastparams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	Raycastparams.IgnoreWater = true

	local camera = workspace.CurrentCamera
	local viewportCenter = Vector2.new(camera.ViewportSize.X/2, camera.ViewportSize.Y/2)
	local viewportRay = camera:ViewportPointToRay(viewportCenter.X, viewportCenter.Y)
	local raycastResult = workspace:Raycast(viewportRay.Origin, viewportRay.Direction, Raycastparams)
3 Likes

Well why not just using CurrentCamera.CFrame.Position. CFrame is always the middle!

CurrentCamera.CFrame.Position is the position of the camera in the 3D world, not the middle of the player’s screen.

1 Like

I ended up doing this and it worked

local Raycastparams = RaycastParams.new()
	Raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
	Raycastparams.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	Raycastparams.IgnoreWater = true

	local camera = workspace.CurrentCamera
	local viewportCenter = Vector2.new(camera.ViewportSize.X/2, camera.ViewportSize.Y/2)
	local viewportRay = camera:ViewportPointToRay(viewportCenter.X, viewportCenter.Y)
	local raycastResult = workspace:Raycast(viewportRay.Origin, viewportRay.Direction * 1000, Raycastparams)

3 Likes

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