private void RecalcNavAgentPath(NavAgent agent, Vector2?destination) { if (destination.HasValue == false) { agent.UpdatePath(null); lastCalculatedDestinations[agent] = null; return; } // If the new destination is not far enough apart from the last one -> do nothing if (lastCalculatedDestinations.TryGetValue(agent, out Vector2? lastDestination)) { if (lastDestination.HasValue) { if (Vector2.Distance(destination.Value, lastDestination.Value) < pathRecalculationThreshold) { return; } } } Vector2 agentPos = agent.transform.position; IList <Vector2> newPath = GetPathTo(agentPos, destination.Value); agent.UpdatePath(newPath); lastCalculatedDestinations[agent] = destination; }