Thank you for looking at my question, I'll dive right into it

The Objective Of Script

The objective of this script is to build a Pathfinding script that creates nodes from the "Start" to the objective (which is called 'Goal' in explorer) and changes the nodes into a new position when Goal is moved to a new location or an object is blocking a path between Start and Goal. (Got this from a tutorial.)

The Problem

The nodes are created from "Start", however, the nodes do not point directly towards Goal, and rather just point off to Vector3.new(0, 0, 0) and it doesn't even reach it either. I looked throughout the entire script for any spelling, math symbols, or punctuation but it must've slipped my eyes or there isn't any. Here's my script

AI PathFinding

local node_Spacing = 2

local open_List = {workspace.Start}
local closed_List = {}

local goal = workspace.Goal
local start = workspace.Start

local valid_Directions = {
	Vector3.new(1, 0, 0), Vector3.new(-1, 0, 0)
, Vector3.new(1, 0, 1) , Vector3.new(-1, 0, -1)
, Vector3.new(1, 0, -1) , Vector3.new(-1, 0, 1)
, Vector3.new(0, 0, 1) , Vector3.new(0, 0, 1)

}


function create_Part(position, parent)
	local part = Instance.new("Part")
	part.Name = "Node"
	part.BrickColor = BrickColor.Blue()
	part.Position = position
	part.Size = Vector3.new(1, 1, 1)
	part.Anchored = true
	part.CanCollide = false
	part.Parent = parent
	
	local G = Instance.new("IntValue")
	G.Name = "G"
	G.Value = parent.G.Value + 1
	G.Parent = part
	
	return part
	
end

function cast_Ray(start, direction)
	return workspace:FindPartOnRay(Ray.new(start, direction * node_Spacing))    
end

function trace_Path(final_Node)
	local current = final_Node
	repeat
		current.BrickColor = BrickColor.Yellow()
		current = current.Parent
	until current.Parent == workspace		
end

function scan_Field()
	local final = nil
	repeat
		local low_F = math.huge
		local best = nil
		for l,node in ipairs(open_List) do
			local H = (node.Position - goal.Position).Magnitude * node_Spacing
			local G = node.G.Value
			if G + H < low_F then
				best = {node, 1}
			end
		end
		table.insert(closed_List, best[1])
		if best[1] ~= start then
			best[1].BrickColor = BrickColor.Blue()
		end
		table.remove(open_List, best[2])
		
		for _,vector in ipairs(valid_Directions)do
			local ray_Part, ray_Pos = cast_Ray(best[1].Position, vector)
			if not ray_Part then
				local part = create_Part(ray_Pos, best[1])
				table.insert(open_List, part)				
			end
		end
		final = best[1]
	until #open_List <= 0 or (best[1].Position - goal.Position).Magnitude <= node_Spacing * 1.5
	trace_Path(final)
end

scan_Field()

print ("Finish Cycling...")

Thanks in advance!

Links

1 comment

0