So I’ve been experimenting with pathfinding on NPCs recently, and it seems as if all the movement of my NPCs are becoming choppy as they keep on moving. https://gyazo.com/c5b1bacbd724dd600231aabce434ebba
I’ve tried viewing other posts on the forum, but none of them really helped with my specific problem. What I’m trying to achieve is obviously a smoother and cleaner look to the walk of the NPC.
Now, what I’m basically doing with the NPCs is that I’m spawning a recursive function that decides and moves the NPC to a specific location. Frankly, I don’t know if it’s a good idea to spawn recursive functions, so if anyone has alternatives, that would be great. Anyways, in the function, I would then start to decide the destination of the NPC, and the path it would take. The function is listed below.
function NPC:ExecuteBehavior()
if self.Type == (“Roamer”) thenlocal EligibleSpawns = {} for i, Spawn in pairs(Workspace:WaitForChild("MovePoints"):GetChildren()) do if (Spawn) ~= (self.Target) then if (self.Target.Position.X) == (Spawn.Position.X) then table.insert(EligibleSpawns, Spawn.Name) end if (self.Target.Position.Z) == (Spawn.Position.Z) then table.insert(EligibleSpawns, Spawn.Name) end end end
local RandomSpawn = Workspace:WaitForChild(“MovePoints”):FindFirstChild(EligibleSpawns[math.random(1, > #EligibleSpawns)])
self.Target = RandomSpawn local Path = self:ComputePath(RandomSpawn) for i, Waypoint in ipairs(Path:GetWaypoints()) do self.NPC.Humanoid:MoveTo(Waypoint.Position) self.NPC.Humanoid.MoveToFinished:Wait() end self:ExecuteBehavior()
end
end
Also, here’s the script executing the functions.
for i = 1, 20 do
local b = a.new(“Roamer”)
spawn(function()
b:ExecuteBehavior()
end)
end
How would I be able to get rid of the choppiness of the NPCs? Also, any other feedback would be appreciated.