- Home /
Question by
lordmonkey · Jan 08, 2013 at 11:58 PM ·
pathfindingeventcreatemessageobstacle
Is there a message/event that I can wait for when object is created in the scene ?
I have written A* pathfinding algorithm. I have my obstacles defined as GameObjects with Tag Obstacle
. I would like to update the obstacle map every seconds or so IF an obstacle has been moved or created/deleted.
I am now doing something like that :
if (stopwatch.Elapsed.TotalMilliseconds - lastMapUpdateInMs > PathFinderConstants.timeForObstaclesUpdateInMs) {
int i = 0;
foreach (GameObject go in obstacles_) {
if ( go.transform.position != obstaclesCachedPos_ [i] ||
go.transform.rotation != obstaclesCachedRot_ [i]) {
regenerateMap = true;
obstaclesCachedPos_ [i] = go.transform.position;
obstaclesCachedRot_ [i] = go.transform.rotation;
break;
}
i++;
}
// this only works for dynamic adding of obstacles, to be able to remove them change that
GameObject[] goarr = GameObject.FindGameObjectsWithTag (PathFinderConstants.obstacleTag);
if (goarr.Length != obstacles_.Count) {
regenerateMap = true;
obstacles_.Add (goarr [goarr.Length - 1]);
obstaclesCachedPos_.Add (goarr [goarr.Length - 1].transform.position);
obstaclesCachedRot_.Add (goarr [goarr.Length - 1].transform.rotation);
}
lastMapUpdateInMs = stopwatch.Elapsed.TotalMilliseconds;
}
But this slows down the application considerably. How con I do this in other way ?
Comment
why you must check the Array size ? You can't switch regenerate$$anonymous$$ap to true when you create or delete an object ? You probably know when this happen ?
Your answer
Follow this Question
Related Questions
Navmesh cut holes 1 Answer
Trouble with Navmesh Agent and Navmesh Obstacle 0 Answers
Need help with my #c script to Generate 2 Obstacles 2 Answers