This is the situation: NPC has spotted an enemy. To protect his trench, he needs to traverse it without jumping out on his way to the enemy.
I have made path points (P14, P13, etc.) for the NPC to follow on his way to the enemy. Upon reaching the closest path point to the enemy, the NPC simply needs to follow the enemy alone.
I am struggling to realize this through code though. This is sort of the idea I was going for:
local pathway = workspace.Pathway
local function findNearestPoint(position) -- Used to find the closest pathpoint...
local closestPoint, closestMag = nil, nil
for i,point in pairs(pathway:GetChildren()) do
local mag = (position - point.Position).Magnitude
if closestMag == nil or mag < closestMag then -- If the closestMag hasn't been declared yet, or if the new mag is THE closest one so far...
closestPoint, closestMag = point, mag
end
end
return closestPoint -- return the closest point!
end
local start, finish = findNearestPoint(NPC_Position), findNearestPoint(Enemy_Position) -- Find the closest points to the NPC Position and the Enemy Position...
-- since pathpoints are named in a chronological order, loop runs through points from start to finish.
for count = string.gsub(start, "P", ""), string.gsub(finish, "P", "") do
local point = pathway["P"..count]
humanoid:MoveTo(point)
humanoid.MoveToFinished:Wait()
end
Any help is HIGHLY appreciated! Thank you in advance!