Search Unity

Question AI Pathfinding moves to navMeshLink and does not take shortest path

Discussion in 'Navigation' started by nichtey, Sep 29, 2020.

  1. nichtey

    nichtey

    Joined:
    Dec 27, 2013
    Posts:
    1
    Hi, I currently have a character that uses the NavMeshAgent component, and the character moves to the position on the land or floor (done by raycasting). As the floor is elevated from the land, navmesh links are used between the floor and the land.

    As shown in the video (First 20 seconds), the character moves to the navMeshLink when it is trying to move from one point to the other, although the expected behaviour is a straight path between the two points. In another case shown in the video (50 - 60 seconds), the character moves as expected. Any suggestions and advice would be greatly appreciated.

    UPDATE: Upon closer examination of the navigation gizmos, it appears that the path polygon in the first case was different from that of the second case, although both gameObjects are identical and should behave in the same way.

    Video Link:


    Nav Mesh Agent settings:
    upload_2020-9-29_20-42-28.png

    Script controlling agent movement

    Code (CSharp):
    1.     public void MoveToSelectedPosition(Vector3 targetPosition)
    2.     {
    3.         navMeshAgent.SetDestination(targetPosition);
    4.         InvokeRepeating("CheckReachedDestination", 0.2f, 0.2f);
    5.         PlayAnimation("Walk");
    6.     }
    7.     public void CheckReachedDestination()
    8.     {
    9.         if (navMeshAgent.enabled == true)
    10.         {
    11.             if (navMeshAgent.remainingDistance < navMeshAgent.stoppingDistance)
    12.             {
    13.                 PlayAnimation("Idle");
    14.                 CancelInvoke("CheckReachedDestination");
    15.             }
    16.             else if (navMeshAgent.remainingDistance < navMeshAgent.stoppingDistance + 0.35f)
    17.             {
    18.                 PlayAnimation("Idle");
    19.             }
    20.         }
    21.         else
    22.         {
    23.             CancelInvoke("CheckReachedDestination");
    24.         }
    25.     }
     
    Last edited: Oct 4, 2020