Raycasting issues when there's a part infront of it

So I’ve visualized the rays here, and when there’s a wall in front of the ray, only one ray works which is the head one, it’s always the head one too, not sure why. Here’s a video of it

(look at the output btw, it shows when its detecting me)


	local ray = Ray.new(origin.Position,origin.CFrame.lookVector * DISTANCE) -- Calculating the raycast.
	local ray2 = Ray.new(origin2.Position,origin2.CFrame.lookVector * DISTANCE)
	local ray3 = Ray.new(origin3.Position,origin3.CFrame.lookVector * DISTANCE)

	local hit = workspace:FindPartOnRay(ray, origin.Parent) -- Creating a ray to see what the npc sees.
	local hit2 = workspace:FindPartOnRay(ray2, origin2.Parent)
	local hit3 = workspace:FindPartOnRay(ray3, origin3.Parent)
	
	local part = Instance.new("Part")
	part.Parent = script.Parent.Head
	part.Anchored = true
	part.CanCollide = false
	part.Size = Vector3.new(0.1,0.1,DISTANCE)
	part.CFrame = CFrame.new(ray.Origin + ray.Direction/2,ray.Origin + ray.Direction)
	
	local part2 = Instance.new("Part")
	part2.Parent = script.Parent["Left arm"]
	part2.Anchored = true
	part2.CanCollide = false
	part2.Size = Vector3.new(0.1,0.1,DISTANCE)
	part2.CFrame = CFrame.new(ray2.Origin + ray2.Direction/2,ray2.Origin + ray2.Direction)
	
	local part3 = Instance.new("Part")
	part3.Parent = script.Parent["Right arm"]
	part3.Anchored = true
	part3.CanCollide = false
	part3.Size = Vector3.new(0.1,0.1,DISTANCE)
	part3.CFrame = CFrame.new(ray3.Origin + ray3.Direction/2,ray3.Origin + ray3.Direction)
	
	if hit or hit2 or hit3 then  
		for _, player in pairs(Players:GetPlayers()) do -- We're going to loop through all players to see if the npc sees any of them.
			if player.Character and player.Character:IsAncestorOf(hit or hit2 or hit3) then 
				-- Handle what happens here.
					print("I see " .. player.Name)
					break
				end
			end
		end
	end

Real horrible code ik lol

WorldRoot:FindPartOnRay is deprecated. You should use WorldRoot:Raycast.


But to help your issue, you can try printing the values of hit or hit2 or hit3. Or:

local obj = hit or hit2 or hit3

if obj then  
		for _, player in pairs(Players:GetPlayers()) do -- We're going to loop through all players to see if the npc sees any of them.
			if player.Character and player.Character:IsAncestorOf(obj) then 
				-- Handle what happens here.
					print("I see " .. player.Name)
					break
				end
			end
		end
	end

Not sure how much this helps you.

1 Like

I don’t think you understand what the issue here is, When there’s a wall and it’s touching the raycasts, the other 2 raycasts aren’t working only the middle one is for some reason, when the wall isn’t there, they’re all working

Try this code

local Run = game:GetService("RunService")

local Dummy = script.Parent

local Head = Dummy.Head
local RightArm = Dummy["Right Arm"]
local LeftArm = Dummy["Left Arm"]

local Distance = 50

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist

local filterDescendants = {}

while true do
	
	for _, player in pairs(Players:GetPlayers())do
		if player.Character then
			if not table.find(filterDescendants, player.Character) then
				table.insert(filterDescendants, player.Character)
			end
		end
	end
	
	raycastParams.FilterDescendantsInstances = filterDescendants
	
	local headRay = workspace:Raycast(Head.Position, Head.CFrame.LookVector * Distance, raycastParams)
	local rightArmRay = workspace:Raycast(RightArm.Position, RightArm.CFrame.LookVector * Distance, raycastParams)
	local leftArmRay = workspace:Raycast(LeftArm.Position, LeftArm.CFrame.LookVector * Distance, raycastParams)
	
	local ray = headRay or rightArmRay or leftArmRay
	
	if ray then
		local rayInstance = ray.Instance
		local player = Players:GetPlayerFromCharacter(rayInstance.Parent)
		
		if player then			
			print("I see " .. player.Name .. " with object : "..rayInstance.Name)
		end
	end
	
	
	Run.Stepped:Wait()
end