How to get 3d position from middle of screen

Hello! I am making a shooting game. On pc if you click on your screen something will shoot towards your mouse position. But on mobile i must use a button to fire it. But the bullet will fire towards the button instead of the middle because the mouse is on the button of course. I’ve tried to make it mouse locked but it doesn’t work. I also read this post but idk how to convert that into the middle of the screen.

Summarize: I want to know the CFrame of the world that the middle of a user’s screen is pointing at. THanks!!!

1 Like

You can use the cameras lookvector to cast a ray, and then use the position of the result

how to use the camera’s lookvector? Oh and by the way I need the CFrame of the part, not the position

workspace.CurrentCamera.CFrame.LookVector will get you the lookvector
are you trying to get the part the player is looking at or the position the player is looking at?

the CFrame of the position the player is looking at

where exactly is said cframe meant to be facing

You can just do camera.CFrame.Position

A more complicated way is to get the camera screen size, get the middle by multiplying by 0.5 then using camera:ScreenPointToRay then use Ray.Origin. It’s the same which is pretty interesting.

local camera = workspace.CurrentCamera
local screenSize = camera.ViewportSize
local screenRay = camera:ScreenPointToRay(screenSize.X*0.5, screenSize.Y*0.5)
print(screenRay.Origin) --Both similar

--Or simply just
print(camera.CFrame.Position)
1 Like

I had the same issue with my placement module v3. I solved it using this method.