I don't, for the life of me, know why my AI seems to 'hang' - that is, whenever it traverses a computed path, it has a tendency to stop.
At first, I thought it might be the terrain - but the navigation mesh and the fact that it would occur even without terrain convinced me otherwise.
local function CalculatePath(Start, Goal) local Path = PathfindingService:ComputeRawPathAsync(Start, Goal, 750) return Path:GetWaypoints(); end local function ChaseTarget(Zombie) local Distance; Zombie.StopFollowing = false; local Points = CalculatePath(Zombie.Root.Position, Zombie.Target.Torso.Position); for i = 1, #Points do if Points[i].Action == Enum.PathWaypointAction.Walk then Zombie.Humanoid:MoveTo(Points[i].Position); else Zombie.Humanoid.Jump = true; end repeat Distance = (Zombie.Root.Position - Points[i].Position).magnitude; if Zombie:IsTargetInSight() then Zombie.StopFollowing = true; end wait(); until Distance <= 5 or Zombie.StopFollowing; if Zombie.StopFollowing then Zombie.StopFollowing = false; break; end end end function Zombie:ChaseTarget() while self.Model do self:GetClosestTarget(); self:PlayAnimation("Run"); if self:IsTargetInSight() then self.Humanoid:MoveTo(self.Target.Torso.Position); else ChaseTarget(self); end wait(1); end end
Just so you get the gist without having to read the code too thoroughly - the AI finds and chases the closest target. It first checks if the target is in sight, to prevent the need to compute a path, otherwise, continues computing one.
As a path is computed in ChaseTarget()
, the AI is made to follow the waypoints. In order to progress to the next waypoint, the AI must reach the current waypoint. If the target is within sight during the iteration, it prematurely stops it and repeats until the AI is dead.
Am I doing something obviously wrong?