Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I use ScreenPointToRay?

Asked by 8 years ago
Edited 8 years ago

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


1 answer

Log in to vote
1
Answered by
Etheroit 178
8 years ago
Edited 8 years ago
print(c:ScreenPointToRay(0,0).Direction)

Will display Vector3 that You are looking at Edit: Here is the workspace location point

print(c.CFrame * c:ScreenPointToRay(0,0).Direction)
0
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. AstrealDev 728 — 8y
0
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 Etheroit 178 — 8y
0
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 Etheroit 178 — 8y
0
ill post a script which will give You workspace location of it wait a minute Etheroit 178 — 8y
View all comments (3 more)
0
That gives me the position of the Camera. Not the workspace location of where my camera is looking AstrealDev 728 — 8y
0
it gives location in workspace where camera is looking cause camera location has been comoared with direction. Etheroit 178 — 8y
0
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 Etheroit 178 — 8y
Ad

Answer this question