How to limit an angle of a ray in raycast

Hello!

I recently wanted to learn how to use raycast and I got an idea to create some sort of mechanic like wheeping angel (basically if you look at the guy, he doesn’t move, you don’t - he moves)

I’ve checked multiple other tutorials and stuff and I seem to don’t really know how to limit the angle ray can be casted… for example, here’s the script I have (following @GnomeCode tutorial on YouTube):

-- I don't know what i am doing
-- ok so i think i need directions, which is a player and a guy, or a character, i dont know

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
		while true do
			-- Default variables
			local head = character:WaitForChild("Head")
			local a = head -- the player itself
			local b = game.Workspace.weeping -- the target
			
			--raycast related variables
			local Direction = b.Position - a.Position
			local params = RaycastParams.new()
			params.FilterType = Enum.RaycastFilterType.Exclude
			params.FilterDescendantsInstances = {workspace.Window}
			
			--smth
			local raycastResult = workspace:Raycast(a.Position, Direction, params)
			
			--doing the dodo
			if raycastResult and raycastResult.Instance then
				if raycastResult.Instance == b then -- IF THE PART IS TARGETED!!
					b.Color = Color3.new(1,0,0)
					print("seen")
				else
					b.Color = Color3.new(0,1,0)
					print("not seen")
				end
			end
			task.wait(0.1)
		end
	end)
end)

Yeah, but the issue right now is that it doesn’t really know where I’m looking at. Sure, the code do ignores the walls and stuff, but as I said, it’s not really something I’m looking for, so, the question is:

How can I check if player looks at the target? Is it even possible using raycasting?

2 Likes