Looking for best way to check if player is looking at object

I’m not sure how to do this in an efficient way, if this isn’t, but currently I’m unsure. I basically just want to check if the player is looking at an object (can tweak fine details later)

The only way I can really think about doing this would be hooking up a render stepped or a while loop and raycasting, which I feel like could be very inefficient. Thanks for any help you can give.

@JustCrock WorldToViewportPoint can be good for this. You can raycast from the position toward the object and see if the ray hits anything if yes then the player can see it! If you don’t understand what I mean. Check out this post for further detail about WorldToViewportPoint and how you can use it to check if a player is looking at something. Check if an object is in the screen - Help and Feedback / Scripting Support - DevForum | Roblox

I want to do this, but not just if the object is in screen, only if the player is like directly looking in its direction.

You can still achieve that using this I’m pretty sure.

Well you can use the code people give (The one that checks if players are looking at objects) and then maybe you can use the instance and check it’s name. If there is a Instance provided for the ray function.

Your basically checking if people are looking at stuff and then you check that things name to know what it is.

Ok cool, I’ll look into it. Thanks for helping me out, and I’ll let you know how it works.

Alright awesome! Glad to help you!

1 Like

You can thank @CoolBrick_1 . He wrote this on the article I sent. The only thing you need to modify
is the first line! Please let me know if it works or not. If not I will modify it and fix it to work.

local Part = workspace.TargetObject -- This is the part I want this script to check if is visible
local Camera = workspace.Camera
local Character = game:GetService('Players').LocalPlayer.Character

local RunService = game:GetService('RunService')

local Parameters = RaycastParams.new()
Parameters.FilterDescendantsInstances = {Character, Part}
Parameters.FilterType = Enum.RaycastFilterType.Blacklist

RunService.RenderStepped:Connect(function()
	local Vector, OnScreen = Camera:WorldToViewportPoint(Part.Position)

	if OnScreen then
		if workspace:Raycast(Camera.CFrame.Position, Part.Position - Camera.CFrame.Position, Parameters) == nil then
			warn('Part is Onscreen')
		end
	end
	
end)

You can use Mouse.Target to know the position where the Mouse is pointing

local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()

while task.wait() do
	print(Mouse.Target)
end

Or you can shoot a raycast in the direction the camera is looking.

local Camera = workspace.CurrentCamera
local Player = game:GetService("Players").LocalPlayer

while task.wait() do
	local IgnoreParams = RaycastParams.new()
	IgnoreParams.FilterType = Enum.RaycastFilterType.Blacklist
	IgnoreParams.FilterDescendantsInstances = {Player.Character}
	local RayResult = workspace:Raycast(Camera.CFrame.Position, Camera.CFrame.LookVector*100, IgnoreParams)
	if RayResult then
		print(RayResult.Instance)
	end
end