Search Unity

AI Pathfinding - Seeker Issues

Discussion in 'Scripting' started by Sidonus-Primus, Nov 26, 2014.

  1. Sidonus-Primus

    Sidonus-Primus

    Joined:
    Nov 26, 2014
    Posts:
    2
    Hi everyone, I am struggling to understand what I am doing wrong. I am attempting to do AI pathfinding tests using Aron Granberg's AI Pathfinding project. My goal is to run many AI entities and test the performance of the frame rate. I want them also to traverse back and forth between two points. The problem I have run into is where I get to a certain number of entities, they stutter as they traverse the path, and the console notifies me of the following:

    "Canceled path because a new one was requested.
    This happens when a new path is requested from the seeker when one was already being calculated.
    For example if a unit got a new order, you might request a new path directly instead of waiting for the now invalid path to be calculated. Which is probably what you want.
    If you are getting this a lot, you might want to consider how you are scheduling path requests."

    If anyone is experienced with this sort of issue, the help would be much appreciated. My code for the AI is shown below. Thanks!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. //Note this line, if it is left out, the script won't know that the class 'Path' exists and it will throw compiler errors
    4. //This line should always be present at the top of scripts which use pathfinding
    5. using Pathfinding;
    6. public class AstarAI : MonoBehaviour {
    7.     //The point to move to
    8.     public Vector3 targetPosition;
    9.    
    10.     private Seeker seeker;
    11.     private CharacterController controller;
    12.    
    13.     //The calculated path
    14.     public Path path;
    15.    
    16.     //The AI's speed per second
    17.     public float speed = 10;
    18.    
    19.     //The max distance from the AI to a waypoint for it to continue to the next waypoint
    20.     public float nextWaypointDistance = 3;
    21.    
    22.     //The waypoint we are currently moving towards
    23.     private int currentWaypoint = 0;
    24.     public float repathRate = 0.5f;
    25.     private float lastRepath = -9999;
    26.     private bool isPositive = false;
    27.    
    28.     public void Start () {
    29.         seeker = GetComponent<Seeker>();
    30.         controller = GetComponent<CharacterController>();
    31.         //Start a new path to the targetPosition, return the result to the OnPathComplete function
    32.         //seeker.StartPath (transform.position,targetPosition, OnPathComplete);
    33.     }
    34.     public void OnPathComplete (Path p) {
    35.         p.Claim (this);
    36.         if (!p.error) {
    37.             if (path != null) path.Release (this);
    38.             path = p;
    39.             //Reset the waypoint counter
    40.             currentWaypoint = 0;
    41.         } else {
    42.             p.Release (this);
    43.             Debug.Log ("Oh noes, the target was not reachable: "+p.errorLog);
    44.         }
    45.        
    46.         //seeker.StartPath (transform.position,targetPosition, OnPathComplete);
    47.     }
    48.    
    49.     public void Update () {
    50.         if (Time.time - lastRepath > repathRate && seeker.IsDone()) {
    51.             lastRepath = Time.time+ Random.value*repathRate*0.5f;
    52.             seeker.StartPath (transform.position,targetPosition, OnPathComplete);
    53.         }
    54.        
    55.         if (path == null) {
    56.             return;
    57.         }
    58.        
    59.         if (currentWaypoint > path.vectorPath.Count) return;
    60.        
    61.         if (currentWaypoint == path.vectorPath.Count) {
    62.             Debug.Log ("End Of Path Reached");
    63.             currentWaypoint++;
    64.                   //This section is what allows the AI to traverse the plane back and forth
    65.             if(isPositive)
    66.             {
    67.                 targetPosition = new Vector3(targetPosition.x+60, 2, (targetPosition.z));
    68.                 isPositive = false;
    69.                 seeker.StartPath(transform.position, targetPosition, OnPathComplete);
    70.             }
    71.             else
    72.             {
    73.                 targetPosition = new Vector3(targetPosition.x-60, 2, (targetPosition.z));
    74.                 isPositive = true;
    75.                 seeker.StartPath(transform.position, targetPosition, OnPathComplete);
    76.             }
    77.             return;
    78.         }
    79.         //Direction to the next waypoint
    80.         Vector3 dir = (path.vectorPath [currentWaypoint] - transform.position).normalized;
    81.         dir *= speed;// * Time.deltaTime;
    82.         //transform.Translate (dir);
    83.         controller.SimpleMove (dir);
    84.        
    85.        
    86.         //if (Vector3.Distance (transform.position,path.vectorPath[currentWaypoint]) < nextWaypointDistance) {
    87.         if ( (transform.position-path.vectorPath[currentWaypoint]).sqrMagnitude < nextWaypointDistance*nextWaypointDistance) {
    88.             currentWaypoint++;
    89.             return;
    90.         }
    91.     }
    92. }
     
  2. Sidonus-Primus

    Sidonus-Primus

    Joined:
    Nov 26, 2014
    Posts:
    2
    bumped. I could really use any sort of help with thus. Every little bit helps.