How to detect if tool can see part with raycast?


The title says it all this is what i tired

while wait() do
	local Handle = script.Parent.Handle	
	
	local RayCastParamaeters = RaycastParams.new()
	RayCastParamaeters.FilterDescendantsInstances = {}
	RayCastParamaeters.FilterType = Enum.RaycastFilterType.Blacklist
	
	local RayDirection = Handle.CFrame.LookVector * 10
	
	local CastedRay = workspace:Raycast(Handle.Position, RayDirection, RayCastParamaeters)
	if CastedRay then
		if CastedRay.Instance.Name == "Wendigo" then
			print(CastedRay.Instance.Name)
		end
	end
end

You’ll want to cast to the part you want to see if the tool can see. A part’s LookVector is a relative direction of the part that points forward.

function canSeePart(handle, targetPart)
	local origin = handle.Position;
	local direction = CFrame.new(origin, targetPart.Position).LookVector; --Creates a LookVector that faces the target part from the origin
	direction *= 20; --Change 20 to the maxiumum distance you want to be able to raycast.
	
	local RayCastParamaeters = RaycastParams.new()
	RayCastParamaeters.FilterDescendantsInstances = {}
	RayCastParamaeters.FilterType = Enum.RaycastFilterType.Blacklist
	
	local result = workspace:Raycast(origin, direction, RayCastParamaeters);
	
	return result and result.Instance == targetPart or false;
end

wait but is there any way of making it still see if the player is not facing it, cause i want it too not detect if the players behind a wall

Yes, the return statement verifies if the part that the raycast hit is the target part.


I mean like i want it too be true in all directions except if the players behind a wall

Well yes, assuming the player is in range, and there’s no other parts in the way, it should work.

1 Like

for this, is this supposed to be the specific part you want to raycast to see if the ray hits it? if so, how can the ray detect if it hits any type of part?

Yes, and you can add your own blacklist if necessary.

so your saying to make it so the ray detects just any part i have to use a blacklist?