Search Unity

NavMeshAgent AI getting stuck on plain ground

Discussion in 'Scripting' started by egartnuc, Nov 28, 2018.

  1. egartnuc

    egartnuc

    Joined:
    Jun 2, 2018
    Posts:
    23
    Hi,

    I have an issue I've been struggling with back and forth for a while, coming back to now and then.

    I have a NavMeshAgent with a script for its' behaviour, in which there is a Evade()-method. The problem is that whenever this is running, after a while (usually a few seconds) the agent gets stuck.

    The destination of the agent is based on the player, as you will see, which also allows the agent to get unstuck if I move the player object. So basically, as long as my character don't stand still, the agent will keep moving.... 99% of the time.

    I use the following code for the evade-method:

    Code (CSharp):
    1. Vector3 runDir = transform.position - player.transform.position;
    2. newPosition = transform.position + runDir;
    3. agent.SetDestination(newPosition);
    I have of course been messing around with the agent settings: the pathfinding, obstacle avoidance and priority parameters, to no help. What I do notice is that sometimes, if I change radius to a higher value the agent changes the direction on its' own and gets unstuck that way. But if I hardcode that in it just gets stuck again instantly since it returns the radius value instantly, or if I'd put it in a coroutine with a delay it would appear, with its' animation, to be stuck anyhow (however slightly) and just not seem natural.

    Any suggestions? Appreciate the help.

    BR,
    Tomas
     
  2. egartnuc

    egartnuc

    Joined:
    Jun 2, 2018
    Posts:
    23
    Wow. Came up with the solution, I think, during lunch break. The agent stops because its in its max distance from player, at its destination.

    It explains all behaviours. Oh well.