我正在使用从这里获取的 A* 寻路算法http://arongranberg.com/astar/docs/我试图使一个对象从一个随机点移动到一个循环系统中的另一个随机点。这是用于移动对象的代码:我试图将这些点放入一个数组中,但它没有用。作者说如果我想在 AI 到达目的地后有额外的行为,我应该在 OnTargetReached() 方法中编写代码,但我不知道具体该怎么做。如果您有任何想法,即使是最小的想法也会很有帮助。
public virtual void SearchPath () {
//if (target == null)
//{ Debug.LogError ("Target is null, aborting all search"); canSearch = false; return; }
lastRepath = Time.time;
//This is where we should search to
//Vector3 [] position = new Vector3[2];
//position[0] = new Vector3(Random.Range(-2,-7), 0, Random.Range(21,26));
//position[1] = new Vector3(Random.Range(19,23), 0, Random.Range (28,31));
//position[2] =
canSearchAgain = false;
//Alternative way of requesting the path
//Path p = PathPool<Path>.GetPath().Setup(GetFeetPosition(),targetPoint,null);
//seeker.StartPath (p);
//We should search from the current position
seeker.StartPath (GetFeetPosition(),targetPosition);
}
public virtual void OnTargetReached () {
//End of path has been reached
//If you want custom logic for when the AI has reached it's destination
//add it here
//You can also create a new script which inherits from this one
//and override the function in that script
//Vector3 new_targetPosition = new Vector3(Random.Range(19,23), 0, Random.Range (28,31));
//Vector3 new_targetPosition = new Vector3(19,0,28);
seeker.StartPath (GetFeetPosition(),new_targetPosition);
}