You would probably use raycasting, but unlike most y’all, raycasting is not my thing. I have only tried out raycasting a couple of times. I don’t need entire script, but maybe things like what kind of things I would be using, thanks!
I think you could use CFrame.LookVector? But i don’t know.
And you would make a the ray on the characters head right?
I think so, i only recently learned about rays.
how would you do that though?(30 chars)
https://developer.roblox.com/en-us/api-reference/function/Camera/WorldToViewportPoint
It contains a code example that will definitely help you out. It is at the bottom.
It uses the old raycasting API but switching over to the new one isn’t too hard.
local Workspace = game:GetService("Workspace")
local camera = Workspace.CurrentCamera
local function is_point_visible(world_point)
local vector, on_screen = camera:WorldToViewportPoint(world_point)
if on_screen then
local origin = camera.CFrame.Position
local raycast_result = Workspace:Raycast(origin, world_point - origin)
local hit = raycast_result and raycast_result.Instance
if hit then
return false
end
else
return false
end
return true
end
I know you would do this in a serverscript, but instead of that would you do:
game.Players.PlayerAdded:Connect(function(plr)
local origin = plr.Character.Head
end)
Camera API should be used in a local script.
What I want to do is make a ray on the players head though…
Then you would do
local head = player.Character.Head
local raycast_result = workspace:Raycast(head.Position, head.CFrame.LookVector*500)
This will cast a ray 500 studs forward from the head’s position, but you can lower it, or raise it (max ray length is 5000 iirc)
I have a question, does the ray that starts at the players head go forward of the head, or forward and back of the head, or just the back of the head, I only want the first one
So then it would look like this?
game.Players.PlayerAdded:Connect(function(plr)
local head = plr.Character.Head
local raycast_result = workspace:Raycast(head.Position, head.CFrame.LookVector*500)
end)
Yes but the character might not have loaded in the instant the player might have. Use Player.CharacterAdded
to detect when the character is spawned in
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
local head = plr.Character.Head
local raycast_result = workspace:Raycast(head.Position, head.CFrame.LookVector*500)
end)
end)
Ok I tried this but this is what I got: 14:22:40.803 - ServerScriptService.Ray:5: attempt to index nil with ‘Instance’
-- Serverscript in serverscriptservice
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
local head = plr.Character.Head
local raycast_result = workspace:Raycast(head.Position, head.CFrame.LookVector*500)
local hitPart = raycast_result.Instance
if hitPart.Name == "Chair" then
print("Looking at chair")
else
print("Not looking at chair")
end
end)
end)
WorldRoot:Raycast
returns nil if the ray didn’t hit anything.
if raycast_result then
local hit_part = raycast_result.Instance
if hit_part.Name == "Chair" then
print("Looking at chair")
else
print("Not looking at chair")
end
end
No errors, but even though I am clearly looking at the chair, its not printing “Looking at chair” and it doesn’t print “Not looking at chair” when i look away, script:
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
local head = plr.Character.Head
local raycast_result = workspace:Raycast(head.Position, head.CFrame.LookVector*500)
if raycast_result then
local hit_part = raycast_result.Instance
if hit_part.Name == "Chair" then
print("Looking at chair")
else
print("Not looking at chair")
end
end
end)
end)
Because it is casting 500 studs straight. This is why I recommended the raycasting from the camera but in a local script.
how do you do that though , this thing?
local function isPointVisible(worldPoint)
local camera = workspace.CurrentCamera
local vector, onScreen = camera:WorldToViewportPoint(worldPoint)
if onScreen then
local origin = camera.CFrame.p
local ray = Ray.new(origin, worldPoint - origin)
local hit = workspace:FindPartOnRay(ray)
if hit then
return false
end
else
return false
end
return true
end
Then how would you start the ray at the head then?