Вопрос

I am using the A* pathfinding algorithm taken from here http://arongranberg.com/astar/docs/ and I am trying to make an object move from a random point to another random point in a loop system in unity.This is the code used to move the object: I tried to put the points into an array but it didnt work. The author says that If I want additional behaviour after the AI reached its destination I should write the code in the OnTargetReached() method, but I am not sure exactly how to. If you've got any ideas, even the smallest would be very helpful.

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);
}
Это было полезно?

Решение

stick a bunch of nodes in your scene (just empty unity objects) name them node1,2,3,4,5 make your loop / path and number them in order.

Make an pathManager script that has a public transform[] nodeLoop; array and drag your nodes onto the array in order. Now you have a list of node/postions.

Now jsut hook it up to your existing OnTargetReached() make a function that just gets the next node position...

something like this

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

pathmanager has something like this...

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

sorry for the hasty pseudocode, but you should get the idea

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top