I am making a wandering NPC, and the problem I'm having is that it chooses a node it can't get to. How can I check whether a given node is reachable using raycasting? Is my raycasting code in the wrong area? Should I iterate through every node making a raycast on each? How do I add the maze and the player character to the ignore list?

Here is my code:

nodeList = game.Workspace.Nodes:GetChildren()
lastNode = nil

while true do
	wait()
	if lastNode == nil then
		nodePart = nodeList[math.random(1, #nodeList)]
		node = nodePart.Position
		lastNode = nodePart
	elseif lastNode ~= nil then
		while true do
			wait()
			nodePart = nodeList[math.random(1, #nodeList)]
			node = nodePart.Position
			if nodePart ~= lastNode then
				break
			end
		end
	end
	ray = Ray.new(script.Parent.Torso.Position, (node - script.Parent.Torso.Position).unit * 5000)
	part, pos = workspace:FindPartOnRayWithIgnoreList(ray, game.Workspace.Model:GetChildren())
	if part then
		script.Parent.Humanoid:MoveTo(pos)
		script.Parent.Humanoid.MoveToFinished:Wait()
	end
end

3 comments

0

Why not just use Path Finding Service?

0

And this question is broad and not very constructive.

0

Pathfinding service is hard to work around in some cases.