0

我正在使用从这里获取的 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);
}
4

1 回答 1

0

在您的场景中粘贴一堆节点(只是空的统一对象)将它们命名为 node1,2,3,4,5 使您的循环/路径并按顺序编号。

制作一个具有公共 transform[] nodeLoop 的 pathManager 脚本;数组并按顺序将节点拖到数组上。现在你有一个节点/职位列表。

现在 jsut 将它连接到您现有的 OnTargetReached() 制作一个仅获取下一个节点位置的函数......

像这样的东西

void OnTargetReached ()
{
  new_targetPosition = pathManager.m.getNextPathPoint()
}

pathmanager 有这样的东西......

int pathPoint=0;
Vector3 getNextPathPoint()
{
   pathPoint++;
   if(pathPoint >= nodeLoop.length)
       pathPoint=0;
   return nodeLoop[pathPoint];
}

抱歉草率的伪代码,但你应该明白

于 2013-11-16T02:37:00.067 回答