Multithread Pathfinding, Unit can't receive path array result

Clash Royale CLAN TAG#URR8PPPMultithread Pathfinding, Unit can't receive path array result
I have a pathfinder program using A* that i made following a tutorial. It works fine until i tried to make it multithread using async Task. The Thread works and the path is calculated, but when the path result is being passed to the Unit object, for some resaon only one or two Unit that receive the path array. where did i do wrong ?
RequestManager :
public class ThreadRequestManager : MonoBehaviour {
Queue<PathResult> results = new Queue<PathResult>();
static ThreadRequestManager instance;
ThreadPathfinding pathfinding;
private void Awake()
{
instance = this;
pathfinding = GetComponent<ThreadPathfinding>();
}
private void Update()
{
if (results.Count > 0)
{
int itemsInQueue = results.Count;
lock (results)
{
for(int i = 0; i < itemsInQueue; i++)
{
PathResult result = results.Dequeue();
result.callback(result.path, result.success);
}
}
}
}
public static async Task RequestPath(PathRequest request)
{
await Task.Run(() => instance.pathfinding.FindPath(request, instance.FinishedProcessingPath));
}
public void FinishedProcessingPath(PathResult result)
{
lock(results)
{
results.Enqueue(result);
}
}
}
public struct PathResult
{
public Vector3 path;
public bool success;
public Action<Vector3, bool> callback;
public PathResult(Vector3 path, bool success, Action<Vector3, bool> callback)
{
this.path = path;
this.success = success;
this.callback = callback;
}
}
public struct PathRequest
{
public Vector3 pathStart;
public Vector3 pathEnd;
public Action<Vector3, bool> callback;
public PathRequest(Vector3 _start, Vector3 _end, Action<Vector3, bool> _callback)
{
pathStart = _start;
pathEnd = _end;
callback = _callback;
}
}
Units :
public class ThreadUnits : MonoBehaviour {
public Transform target;
float speed = .5f;
public Vector3 path;
int targetIndex;
// Use this for initialization
void Start()
{
ThreadRequestManager.RequestPath(new PathRequest(transform.position, target.position, OnPathFound));
}
public void OnPathFound(Vector3 newPath, bool pathSuccess)
{
if (pathSuccess)
{
path = newPath;
// FollowPath();
StopCoroutine("FollowPath");
StartCoroutine("FollowPath");
}
}
IEnumerator FollowPath()
{
Vector3 currentWaypoint = path[0];
while (true)
{
if (transform.position == currentWaypoint)
{
targetIndex++;
if (targetIndex >= path.Length)
{
yield break;
}
currentWaypoint = path[targetIndex];
}
transform.position = Vector3.MoveTowards(transform.position, currentWaypoint, speed);
yield return null;
}
}
public void OnDrawGizmos()
{
if (path != null)
{
for (int i = targetIndex; i < path.Length; i++)
{
Gizmos.color = Color.black;
Gizmos.DrawCube(path[i], Vector3.one);
if (i == targetIndex)
{
Gizmos.DrawLine(transform.position, path[i]);
}
else
{
Gizmos.DrawLine(path[i - 1], path[i]);
}
}
}
}
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.