Hello there! I’m trying to make A* npc that works in Vector3. I met the problem where point goes further every 2 ticks like in screenshot.
Code I used:
game:GetService('RunService').Heartbeat:Connect(function()
local smallest = {nil,math.huge,math.huge}
for i,v in pairs(open) do
if v[2]+v[3]<smallest[2]+smallest[3] then
local found = false
for index,q in pairs(closed) do
if q[1] == v[1] then
found=true
break
end
end
if not found then
if smallest[3]>v[3] then
smallest=v
end
end
end
end
--if smallest+
local smallGap = Vector3.new(0.0001,0.01,0.0001)
local posTable = {
smallest[1]+Vector3.new(grid,0,0)+smallGap,
smallest[1]+Vector3.new(-grid,0,0)+smallGap,
smallest[1]+Vector3.new(0,0,grid)+smallGap,
smallest[1]+Vector3.new(0,0,-grid)+smallGap,
}
local pointTable = {
{posTable[1]+Vector3.new(grid,0,0),smallest[2]+grid,(posTable[1]-vector).Magnitude},
{posTable[2]+Vector3.new(-grid,0,0),smallest[2]+grid,(posTable[2]-vector).Magnitude},
{posTable[3]+Vector3.new(0,0,grid),smallest[2]+grid,(posTable[3]-vector).Magnitude},
{posTable[4]+Vector3.new(0,0,-grid),smallest[2]+grid,(posTable[4]-vector).Magnitude},
}
for i,v in pairs(pointTable) do
table.insert(open,v)
end
for i,point in pairs(pointTable) do
local raycast = Raycast(self.body,point[1])
if raycast then
local rpos = raycast.Position+Vector3.new(0.0001,0.01,0.0001)
local difference = point-raycast.Position
if hipHeight>difference then
showPoint(rpos,grid,'normal')
pointTable[i] = rpos
elseif difference < jumpPower then
showPoint(rpos,grid,'normal')
showPoint(point[1],grid,'air')
table.insert(pointTable,rpos)
else
showPoint(rpos,grid,'normal')
showPoint(point[1],grid,'air')
table.insert(pointTable,rpos)
end
else
table.remove(pointTable,i)
showPoint(point[1],grid,'air')
end
end
table.insert(closed,smallest)
end)
I tried to add some vector to points and choose the point with same distance+path went with smallest distance. But it doesnt work.