Hi guys,
So I’m a little confused on what Camera:ScreenPointToRay() does. I thought it would essentially take a pixel and lock a ray onto that pixel, hitting whatever object that pixel was showing, allowing you to find the CFrame of that pixel. Here’s a picture of what I thought was happening:
It wasn’t working in my scenario, so I must’ve done something wrong or the function works differently than expected. Here’s my code that works as intended:
wait()
local mouse = game.Players.LocalPlayer:GetMouse()
local CurrentCam = game.Workspace.CurrentCamera
local VPSize = CurrentCam.ViewportSize
while true do
wait(1)
local NewRay = Ray.new(game.Workspace.CurrentCamera.CFrame.Position, CurrentCam.CFrame.LookVector.Unit*5000)--CurrentCam:ScreenPointToRay(0.5*VPSize.X, 0.5*VPSize.Y)
local Part = Instance.new("Part")
local Target, HitPos = workspace:FindPartOnRayWithIgnoreList(NewRay, {game.Players.LocalPlayer.Character})
if Target then
print(Target:GetFullName())
else
print("Did not hit anything.")
end
Part.Position = HitPos
print (Part.Position)
Part.Parent = workspace
Part.Anchored = true
Part.Size = Vector3.new(1,1,1)
Part.BrickColor = BrickColor.new("Bright red")
end
And here is my code using ScreenPointToRay:
wait()
local mouse = game.Players.LocalPlayer:GetMouse()
local CurrentCam = game.Workspace.CurrentCamera
local VPSize = CurrentCam.ViewportSize
while true do
wait(1)
local NewRay = CurrentCam:ScreenPointToRay(0.5*VPSize.X, 0.5*VPSize.Y)
local Part = Instance.new("Part")
local Target, HitPos = workspace:FindPartOnRayWithIgnoreList(NewRay, {game.Players.LocalPlayer.Character})
if Target then
print(Target:GetFullName())
else
print("Did not hit anything.")
end
Part.Position = HitPos
print (Part.Position)
Part.Parent = workspace
Part.Anchored = true
Part.Size = Vector3.new(1,1,1)
Part.BrickColor = BrickColor.new("Bright red")
end
Any ideas on what I did wrong? Thanks!