Pathfinding issue

so im making unit AI, and i encounter to this problem where my units would start flinching on their way to the enemy

it becomes very visible by the end of the video

function pathToTarget(unit)
	local path = game:GetService("PathfindingService"):CreatePath()
	
	local success, errorMessage = pcall(function()
		path:ComputeAsync(unit.root.Position, unit.target.Position)
	end)
	
	local waypoints = path:GetWaypoints()
	
	local nextWaypointIndex

	local reachedConnection
	local blockedConnection
	
	if success and path.Status == Enum.PathStatus.Success then
		blockedConnection = path.Blocked:Connect(function(blockedIndex)
			if blockedIndex >= nextWaypointIndex then
				blockedConnection:Disconnect()
				pathToTarget(unit)
			end
		end)

		if not reachedConnection then
			reachedConnection = unit.human.MoveToFinished:Connect(function(reached)
				if unit.target then
					if reached and nextWaypointIndex < #waypoints then
						nextWaypointIndex += 1
						unit.human:MoveTo(waypoints[nextWaypointIndex].Position)
					else
						reachedConnection:Disconnect()
						blockedConnection:Disconnect()
					end
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end

		nextWaypointIndex = 2
		unit.human:MoveTo(waypoints[nextWaypointIndex].Position)
	else
		reachedConnection:Disconnect()
		blockedConnection:Disconnect()
		warn("target nonexistant")
	end
end

One possible solution would be to have your units “wait” for a short period of time before trying to find a new path. You could do this by adding a brief delay before calling pathToTarget(unit) again

it didn’t fix anything, troops would still flinch like nothing changed

Check if the Blocked event is being triggered too frequently. If the event is being triggered very frequently (e.g. every frame), it could cause the unit’s movement to stutter.

when i put print(blockedConnection) it would print Connection every frame, even when the path isn’t blocked, so im not quite sure its the right way to check if its blocked

print(blockedIndex) prints nothing

any solutions regarding this issue?

i have found the solution:

if success and path.Status == Enum.PathStatus.Success then
		waypoints = path:GetWaypoints()

		unit.human:MoveTo(waypoints[waypointIndex].Position)

		blockedConnection = path.Blocked:Connect(function(blockedIndex)
			if blockedIndex >= waypointIndex then
				blockedConnection:Disconnect()
				infantryToTarget(unit)
			end
		end)

		if not reachedConnection then
			reachedConnection = unit.human.MoveToFinished:Connect(function(reached)
				if unit.target then
					if reached and waypointIndex < #waypoints then
						waypointIndex += 1
					else
						reachedConnection:Disconnect()
						blockedConnection:Disconnect()
					end
				else
					reachedConnection:Disconnect()
					blockedConnection:Disconnect()
				end
			end)
		end
	else
		reachedConnection:Disconnect()
		blockedConnection:Disconnect()
	end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.