Hey there,
I would need your help please.
Every x seconds we check if there are new available jobs in the factory, if so we find the closest worker and give to they the task to complete the job. To make sure the worker is not stuck somewhere we check if the path is valid, if not we look for the next worker. I am a bit worried about the performance impact of such activity but we find it necessary since it happens players stuck the worker and without this check the whole factory fails.
So we do the following check:
public static bool IsPathValid(Vector3 startingPosition, Vector3 destination, float horStopDistance, float vertStopDistance)
{
var node1 = AstarPath.active.GetNearest(startingPosition);
var node2 = AstarPath.active.GetNearest(destination);
if (ReferenceEquals(node1.node, null) || ReferenceEquals(node2.node, null)) return false;
if (!AreTwoPositionsWithinRange(destination, node2.position, horStopDistance, vertStopDistance)) return false;
return PathUtilities.IsPathPossible(node1.node, node2.node);
}
public static bool AreTwoPositionsWithinRange(Vector3 positionOne, Vector3 positionTwo, float horizontalDistance, float verticalDistance)
{
var isHorizontalDistanceInRange = (new Vector2(positionOne.x, positionOne.z) - new Vector2(positionTwo.x, positionTwo.z)).sqrMagnitude <
(horizontalDistance * horizontalDistance);
var isVerticalDistanceInRange = Mathf.Abs(positionOne.y - positionTwo.y) < verticalDistance;
return isHorizontalDistanceInRange && isVerticalDistanceInRange;
}
Do you think this is the most performance efficient approach? We are suffering from some performance issues related to this so we need to limit the number of workers the player can have.
Ps we are on version: 4.3.61
Thanks!!
Paolo