Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I check if a ray hit one of my pathfinding nodes?

Asked by 6 years ago
Edited 6 years ago

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
0
Why not just use Path Finding Service? farrizbb 465 — 6y
0
And this question is broad and not very constructive. farrizbb 465 — 6y
0
Pathfinding service is hard to work around in some cases. ronitrocket 120 — 6y

Answer this question