Hi, here is what i have:
- A Monster in the scene
- A script PathFinder attached to this monster
My pathfinder script add a thread to a threadpool each time it needs to recalculate a path. I have no problem with that, every threads seems to be stopped when the path is calculated.
But, when i put multiple monsters, only 3 or 4 of them are moving and the other ones are just doing nothing and my Unity Process uses 100% of my CPU.
Script attached to the monster:
pathFinder=newPathFinder();
...
if (Vector3.Distance (newTargetPosition, oldTargetPosition) >= 2f) {
if (pathFinder.finished) {
pathFinder.SearchPath (transform.position, newTargetPosition, wmask, ref waypoints);
}
oldTargetPosition = newTargetPosition;
}
....
PathFinder script:
public void SearchPath(...) {
finished = false;
ThreadManager.AddThread (new WaitCallback (Ascend));
}
private void Ascend() {
discoveredList.Add (startNode);
/* JOB */
finished = true;
}
And here is my ThreadPool:
public class ThreadManager : MonoBehaviour {
// Use this for initialization
void Start () {
SetupThreadPool ();
}
void SetupThreadPool() {
ThreadPool.SetMaxThreads (Environment.ProcessorCount, Environment.ProcessorCount);
}
public static void AddThread(WaitCallback b) {
ThreadPool.QueueUserWorkItem (b);
}
}
Thanks for your support !