Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Chasing a target using pathfinding

Discussion in 'Editor & General Support' started by siflandolly, May 2, 2012.

  1. siflandolly

    siflandolly

    Joined:
    May 17, 2011
    Posts:
    141
    What is the recommended way to have an AI chase a target like the player using pathfinding? I am using A* for pathfinding. If the AI is chasing someone who is moving I don't want to keep recalculating a new path each time the target moves. Is there a typical design pattern people follow for this problem?

    I was thinking of trying to have the AI move directly toward the target first without pathfinding, then if it hits something use pathfinding. Or maybe use raycasting to see if something is in between the AI and the target and use pathfinding only if there's something in the way.

    Anyone have any recommendations?
     
  2. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    Construct the first path to target with the A* but don't do another path to target until you reach the first target end point.
    Example, enemy is located in quadrant 3, player is located in quadrant 1, the enemy code creates a node list to target player that is what is stored for the path to take to the last known player position. After T (time) has passed and enemy has moved towards P (player), do another A* node build path to P, repeat at every T interval until P is in range to fight or until P is out of range then return E (enemy) to O (original location)

    You don't need to build the path on every game loop, and you would need to use a ray to determine the distance to target which will help to determine if your bot should continue to pursue or give up.

    Only make path adjustments over time in intervals, not every loop, this saves overhead and you can add prediction, interpolation to the process.


    Just one thought to this problem
     
  3. siflandolly

    siflandolly

    Joined:
    May 17, 2011
    Posts:
    141
    Okay that sounds like a good approach. Thanks :)