Alright, so recently I've been messing around with the Camera. I want to find the 3D position the player is looking at using the camera. This is the code I'm using:
c = game.Workspace.CurrentCamera
c.CameraSubject = game.Workspace.NewCam
c.CameraType = "Attach"
print(c:ScreenPointToRay(0,0))
After I press 'Play' in Studio, I check the coordinates that are printed into the output. This is what gets printed:
{-0.989541292, 30.9928341, 11.3838854}, {-0.759665072, 0.218074307, -0.612660289}
The coordinates are always a bit different. How do I use 'ScreenPointToRay' to find the 3D position the player is looking at?
The full script:
wait()
plr = game.Players.LocalPlayer
inputS = game:GetService("UserInputService")
pressed = false
mouse = plr:GetMouse()
c = game.Workspace.CurrentCamera
c.CameraSubject = game.Workspace.NewCam
c.CameraType = "Custom"
plr.CameraMaxZoomDistance = 0
print(c:ScreenPointToRay(0,0).Direction)
-- ChatScript, CameraScript, ControlScript
plr.PlayerScripts:WaitForChild("ChatScript")
plr.PlayerScripts.ChatScript:Destroy()
plr.PlayerScripts.ControlScript:Destroy()
inputS.InputBegan:connect(function(i, gameProcessedEvent)
if i.KeyCode == Enum.KeyCode.D then
for i=1,15,1 do
game.Workspace.NewCam.CFrame = game.Workspace.NewCam.CFrame + Vector3.new(1,0,0)
wait(0.001)
end
elseif i.KeyCode == Enum.KeyCode.A then
for i=1,15,1 do
game.Workspace.NewCam.CFrame = game.Workspace.NewCam.CFrame + Vector3.new(-1,0,0)
wait(0.001)
end
elseif i.KeyCode == Enum.KeyCode.W then
--for i=1,15,1 do--
game.Workspace.NewCam.CFrame = mouse.Hit
wait(0.001)
--end
elseif i.KeyCode == Enum.KeyCode.S then
for i=1,15,1 do
game.Workspace.NewCam.CFrame = game.Workspace.NewCam.CFrame + Vector3.new(0,-1,0)
wait(0.001)
end
elseif i.KeyCode == Enum.KeyCode.G then
print(c:ScreenPointToRay(0,0).Direction)
end
end)
-- y + 15 in rotation

7 comments
That always prints out a number that's extremely close to 0,0,0 - I keep getting numbers like: 0.2, 0.4, -0.1 - This really isn't the Vector3 that I'm looking at.
it shows where does camera look (camera is on 0,0,0 and it looks at 0.2,0.4,-0.1 point.after some compares You can get where is camera and where is normally (in workspace) point where is it looking at
cause currently its like beign compared already and it just shows direction where does it look (You can do part on these cordinates and if u place camera on 0, 0, 0 and look at it then function will show You this again) It just shows direction. not workspace vector
ill post a script which will give You workspace location of it wait a minute
That gives me the position of the Camera. Not the workspace location of where my camera is looking
it gives location in workspace where camera is looking cause camera location has been comoared with direction.
it gives a point in like 1 stud in front of camera. You cant give further point cause You must check colisions firstly so thats the easiest way to get point where is player looking