So I made an A* Pathfinding algorithm and it works pretty well without walls, when walls come it gets stuck there, i will show some pictures
Script:
local start = workspace.Start
local goal = workspace.Goal
local openlist, checked, waypoints = {}, {}, {}
table.insert(openlist, start)
local validDirections = {
Vector3.new(5,0,0), Vector3.new(-5,0,0),
Vector3.new(0,0,5), Vector3.new(0,0, -5),
Vector3.new(5,0,5), Vector3.new(-5,0,-5),
Vector3.new(-5,0,5), Vector3.new(5,0,-5)
}
local currentNode, Position = start, 1
local goal_Reached = false
repeat
table.remove(openlist, Position)
local bestfcost,bestIndex = 9999,1
for i,v in pairs(validDirections) do
local checkWhetherwallisinfront = workspace:Raycast(currentNode.Position, v)
if checkWhetherwallisinfront and checkWhetherwallisinfront.Instance and checkWhetherwallisinfront.Instance.Parent ~= workspace.Nodes then
print(checkWhetherwallisinfront.Instance)
continue
end
local magnitude = (goal.Position - (currentNode.Position + v)).Magnitude
local part = Instance.new("Part")
part.Parent = workspace.Nodes
part.Position = currentNode.Position + v
part.Anchored = true
local intValue = Instance.new("IntValue")
intValue.Parent = part
intValue.Name = "F"
if v == Vector3.new(5,0,5) or v == Vector3.new(-5,0,-5) or v == Vector3.new(-5,0,5) or v == Vector3.new(5,0,-5) then
part.BrickColor = BrickColor.new("Red flip/flop")
intValue.Value = magnitude + 14
else
intValue.Value = magnitude + 10
end
table.insert(openlist, part)
end
for i,v in pairs(openlist) do
if(v.F.Value < bestfcost) then
bestfcost = v.F.Value
bestIndex = i
elseif v.F.Value == bestfcost then
local magnitude = (v.Position - goal.Position).Magnitude
local magnitude2 = (openlist[bestIndex].Position - goal.Position).Magnitude
if magnitude < magnitude2 then
bestIndex = i
bestfcost = v.F.Value
end
end
end
currentNode = openlist[bestIndex]
currentNode.BrickColor = BrickColor.new("Sea green")
currentNode.Size = Vector3.new(1, 10, 1)
print((currentNode.Position - goal.Position).Magnitude)
table.insert(waypoints, currentNode)
task.wait()
until (currentNode.Position - goal.Position).Magnitude < 3
print("yes")
I think something with the raycasting is wrong, any help?