寻路统一中的环运动系统

时间:2013-11-12 23:05:57

标签: c# unity3d path-finding

我正在使用从这里http://arongranberg.com/astar/docs/取得的A *寻路算法,我试图让一个对象从一个随机点移动到一个循环系统中的另一个随机点。这是用来移动的代码对象:我试图将点放入数组但它没有用。作者说,如果我想在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);
}

1 个答案:

答案 0 :(得分:0)

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

创建一个具有public 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];
}

对于仓促的伪代码感到抱歉,但你应该明白这个想法