Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. The 2022.2 beta is now available for testing. To find out what's new, have a look at our 2022.2 feature highlights.
    Dismiss Notice
  3. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here:
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  5. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice
Dismiss Notice
Submit bug reports tagged with #Beta2022Win_Unity when you encounter unknown issues while testing the 2022.2 betas for a chance to win a year of Unity Pro. For more information, have a look at our Beta Sweepstakes Announcement.

Custom Heuristic?

Discussion in 'AI & Navigation Previews' started by CDF, Feb 22, 2017.

  1. CDF


    Sep 14, 2013
    Hi, was wondering if you guys have thought about adding custom heuristics when finding paths?

    as an example, with a custom heuristic I could drive the direction of found paths to exclude certain directions. Imagine an enemy following a player. The player wants to move behind the enemy to avoid it, but shouldn't turn around and walk towards the enemy. It should keep moving forward and look for a path that leads back around behind the enemy. With a custom heuristic I could increase the cost of a node based on the current path direction of to that node towards the enemy.

    Something like:

    Code (CSharp):
    1. NavMesh.CalculatePath(playerPos, enemyPos - enemy.forward * 3, areMask, CustomHeuristic);
    3. float CustomHeuristic(Vector3[] cornersSoFar, int cornerIndex, Vector3 sourcePosition, Vector3 targetPosition) {
    5.     Vector3 currentCorner = cornersSoFar[cornerIndex];
    6.     Vector3 previousCorner = cornersSoFar[cornerIndex - 1];
    8.     float directionOnEnemy = Vector3.Dot(currentCorner - previousCorner, currentCorner - enemy.position);
    10.     return directionOnEnemy < 0 ? float.PositiveInfinity : (currentCorner - targetPosition).magnitude;
    11. }
    I'm sure you guys could come up with a better callback, maybe passing in a struct of data instead of all those parameters.
    laurentlavigne likes this.